Why prototypes manually initialized to zero still inherit from Object

If i write this

var o = Object.create(null)
alert(o instanceof Object) // this is false

How come it is true?

function o() {

}
o.prototype = null
alert(new o() instanceof Object) // this is true

You should not manually set the prototype to null so that it does not inherit anything like Object.create. Thanks in advance: -)

+3
source share
2 answers

In short, if the prototype constructor is not an object, then Object.prototype is provided as instances of them [[prototype]].

Details are given in ECMA-262, ยง13.2.2 [[Construct]]:

When the internal [[Construct]] method for an object of function F is called with a possibly empty list of arguments, the following steps are performed:

  • Let obj be the just created custom ECMAScript object.
  • Define all internal obj methods as described in 8.12.
  • [[Class]] obj " Object".
  • [[]] obj true.
  • proto [[Get]] F ".
  • Type (proto) - Object, [[Prototype]] obj .
  • (proto) , [[Prototype]] obj , 15.2.4.
  • [[Call]] F, obj this [[Construct]] args.
  • Type (result) - Object, .
  • obj.

, 6 7 null null (ECMA-262 ยง8.2), typeof null, .

+2

, o.

() Object

Prototype

+1

All Articles