Reputation: 7397
If I write this
var o = Object.create(null)
alert(o instanceof Object) // this is false
How come this ends up being true
function o() {
}
o.prototype = null
alert(new o() instanceof Object) // this is true
Shouldn't manually setting the prototype to null cause it to inherit from nothing as Object.create does. Thanks in advance :-)
Upvotes: 4
Views: 228
Reputation: 147363
Briefly, if a constructor's prototype isn't an Object, then instances are given Object.prototype as their [[prototype]].
The detail is in ECMA-262, §13.2.2 [[Construct]]:
When the [[Construct]] internal method for a Function object F is called with a possibly empty list of arguments, the following steps are taken:
- Let obj be a newly created native ECMAScript object.
- Set all the internal methods of obj as specified in 8.12.
- Set the [[Class]] internal property of obj to "Object".
- Set the [[Extensible]] internal property of obj to true.
- Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
- If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
- If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.
- Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
- If Type(result) is Object then return result.
- Return obj.
Noting that in items 6 and 7, null
is Type null (ECMA-262 §8.2), it is not the same as typeof null
, which is object.
Upvotes: 4
Reputation: 490183
When you instantiate it like that, it returns an object of o
.
Its (hidden) prototype chain still points to Object
Upvotes: 1