*.__proto__: the constructor function of an object.
*.prototype: the value of prototype is used to initialize __proto__.
*.constructor: the constructor function to create new object.
> const o = new Object();
> o.__proto__ === Object.prototype
true
> Object.__proto__
[Function] // let's call it func
> const func = Object.__proto__
> Object.prototype
{} // which is an object that has no __proto__, let's call it obj
> const obj = Object.prototype
> Object.constructor
[Function: Function] // yes, it can own new child
> Object.constructor === Function
> Function.__proto__
[Function]
> Function.__proto__ === func
true
> Function.prototype
[Function]
> Function.prototype === func
true
> Function.constructor
[Function: Function]
> Function.constructor === Function
true
> func.__proto__
{}
> func.__proto__ === obj
true
> func.prototype
undefined
> func.constructor
[Function: Function]
> func.constructor === Function
true
> obj.__proto__
null
> obj.prototype
undefined
> obj.constructor
[Function: Object]
> obj.constructor === O
true
> const a = new Object()
> a instanceof Object
true
> a.__proto__
{} // obj
> a.prototype
undefined
> a.constructor
[Function: Object] // Object
const func = Object.__proto__
const obj = Object.prototype
const a = new Object()
pointer | value | typeof | __proto__ | prototype | constructor |
---|---|---|---|---|---|
Object | [Function: Object] | function | func | obj | Function |
Function | [Function: Function] | function | func | func | Function |
func | [Function] | function | obj | Function | |
obj | {} | object | Object | ||
a | {} | object | obj | Object |
a, func, obj cannot be used as right expression of "instanceof"
All names in the table refers to an unique object.
class obj {
constructor() [[native code]]
}
class func extends obj {
constructor() [[native code]]
}
const Object = obj.constructor;
const Function = func.constructor;
> typeof (class A {})
'function'
The class declaration creates a new class with a given name using prototype-based inheritance.
Object is a function;
Object is an instance of Function (
Object is a Function object;
Object is an instance of the Function constructor;
);
Object inherits from Function.prototype aka. func;
func inherits from Object.prototype aka. obj;
obj is an object;
Object inherits from obj;
func is an object;
Function inherits from func;
func inherits from obj.
Function is an instance of itself (naturally, since it’s a function, and thus an instance of Function).
Object ---> Function.prototype ---> Object.prototype ---> null
Function ---> Function.prototype ---> Object.prototype ---> null
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
in another word,
The instanceof operator tests whether the prototype property of an constructor of something exists in a prototype chain of an object.