2

I'm just reading about Prototypes in JavaScript and Douglas Crockford offers and excellent way to select a new objects prototype but can anyone explain (below) why obj01's type equals 'object' when I pass it in function as it's prototype?

if (typeof Object.beget !== 'function') {
     Object.beget = function (o) {
         console.log(typeof o);//function
         var F = function () {};
         F.prototype = o;
         console.log(typeof F);//function
         return new F();
     };
}
var func01 = function(){};
var obj01 = Object.beget(func01);
console.log(typeof obj01);//object
console.log(typeof obj01.prototype);//object

I thought it would be

console.log(typeof obj01);//function
console.log(typeof obj01.prototype);//function

2 Answers 2

4

obj01 is simply an object that inherits from a function object, you can't create functions in this way.

The typeof operator returns "function" only when its operand is by itself callable.

There are only three valid ways to create function objects:

Function declaration:

function name (/*arg, argn...*/) {
}

Function expression:

var fn = function /*nameopt*/ (/*arg, argn...*/) {
};

Function constructor:

var fn = new Function("arg", "argn", "FunctionBody");

Edit: In response to your comment, obj01 , is just an object, its prototype chain contains a function object, then Function.prototype and then Object.prototype but that doesn't make an object callable.

Your object is not callable, functions are just objects, but they have some special internal properties that allow them to behave like that.

An object is callable only if it implements the internal [[Call]] property.

There are other internal properties that function objects have, like the [[Construct]], which is invoked when the new operator is used, the [[Scope]] property which stores the lexical environment where the function is executed, and more.

If you try to invoke your object like if it were a function, you will have a TypeError, because when you make a function call, the object needs to have the [[Call]] internal property.

Function objects need to have the above internal properties, and the only way that they can be constructed is by the three methods I mentioned early, you can see how internally functions objects are created here.

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

5 Comments

Hi CMS, it wasn't that I was trying to create a function. I was trying to understand inheritance and prototyping in JavaScript. So if my new object's prototype is now a Function.prototype why isn't my new object callable? Are the call and apply methods not part of the function's prototype that my new object has inherited?
@Nick: Edited to clarify, please ask if you have any doubt.
Thanks CMS. That was a very thorough answer. I really appreciate your help. I do have one more question. When you talk about an object's prototype chain, is that stored in it's hidden internal prototype property? I know functions have two prototype properties, one hidden and one not.
@Nick, exactly, every object has an internal [[Prototype]] property, this property is the link between objects that builds up the prototype chain, functions have also a prototype property, the object referenced by this property will be used as the [[Prototype]] of every new object created by the function when it is used as a constructor, this is done by the new operator with the help of the [[Construct]] internal operation.
Thank you so much CMS. I'm really starting to understand how JavaScript works.
1

Its really quite simple.

The variable F points to a function, so typeof F returns 'function'.

But the return value from F() is an object, an execution context (activation object), or an instance of a class if you want. For more info on this read this excellent blog series.

Therefor typeof F() returns 'object'.

As Martyn showed in his comment; change return new F(); into return F;. That should return a new function with a modified scope chain usable for instantiating a new 'subclass'.

6 Comments

@Martyn, that misses completely the point of this beget function, create an object that inherits from another...
@Sean, the value of new F(); is simply an object (created by the [[Construct]] internal operation), it has nothing to do with the activation object, which in this case for beget and F will be garbage collected after every invocation.
Its not discarded as long as new is used, and the function does not explicitly call return foo; Of course, it might be that I'm remembering incorrectly and it wasn't the AO but one of the others.
@CMS - yup, I missed the nuances of that question. Apologies for any confusion and thank you for correcting me.
@CMS, Sean, I'm a little confused. Are you saying that when I invoke a Function constructor the object created is not a function?
|

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.