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
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.
5 Comments
[[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.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
beget function, create an object that inherits from another...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.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.