7

I am currently learning Javascript. I've read that an object which has an internal member [[Call]] produces function as the result of typeof that object. I wonder whether I can set this internal member in my Javascript code, i.e. is something like this possible? :

function my_foo(){}

var my_obj = {};  // is the ';' an empty statement?

my_obj["[[Call]]"]=my_foo; // in my test, this didn't work

And if it is possible, would this change the result of typeof that object from object to function?

3
  • Why are you not just using var my_obj = new my_foo(); if you want it of type my_foo extending on function? Commented Aug 2, 2017 at 13:28
  • @MartinBarker: Probably because [[Call]] is defined as an internal method (e.g., a reference to executable code). (It threw me at first too.) Commented Aug 2, 2017 at 13:29
  • @MartinBaker: As far as I can see (I'm learning), with var my_obj = new my_foo(); I cannot do an invocation of the form my_obj(); Suppose I have a function A(f){...}, that invokes the argument f, i.e. a call f(); I can do: function B(){....} B['x']=10; .... A(B); But what, if I have an existing object O with existing properties that I got passed as a parameter into a function of mine. I hoped I could associate the object with a new function body to pass it as an argument to the function A, i.e. that I could call 'A(O)' without an error. Commented Aug 2, 2017 at 22:42

1 Answer 1

8

I've read that an object which has an internal member [[Call]] produces function as the result of typeof that object. I wonder whether I can set this internal member in my Javascript code, i.e. is something like this possible?

No. You cannot directly set or modify internal slots of JavaScript objects. That's part of why they're internal slots, not properties. Your code just creates a property called [[Call]], which is unrelated to the internal slot. From the linked spec section:

Internal slots correspond to internal state that is associated with objects and used by various ECMAScript specification algorithms. Internal slots are not object properties and they are not inherited. Depending upon the specific internal slot specification, such state may consist of values of any ECMAScript language type or of specific ECMAScript specification type values. Unless explicitly specified otherwise, internal slots are allocated as part of the process of creating an object and may not be dynamically added to an object. Unless specified otherwise, the initial value of an internal slot is the value undefined. Various algorithms within this specification create objects that have internal slots. However, the ECMAScript language provides no direct way to associate internal slots with an object.

There's no mechanism defined to let you set [[Call]]. (This isn't universally true of all internal slots; for instance, there's a mechanism that lets you set the [[Prototype]] internal slot of an object: Object.setPrototypeOf. But there isn't one for [[Call]].)

Sign up to request clarification or add additional context in comments.

3 Comments

At rare times i wish i could up vote twice and this is one of them. You provided a good answer then edited to provide an awesome answer. but add your citations for your quotes would you :)
@MartinBarker: Very kind. :-) I'm kind of glad you can't, though; +7 seems plenty upvoted already...
Thank you for the clarification!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.