Skip to content

Instantly share code, notes, and snippets.

@Misaka-0x447f
Last active August 31, 2019 10:55
Show Gist options
  • Save Misaka-0x447f/debc5a6965db3efe9c4b53cbf68b054e to your computer and use it in GitHub Desktop.
Save Misaka-0x447f/debc5a6965db3efe9c4b53cbf68b054e to your computer and use it in GitHub Desktop.
Learn about the instanceof, the Function, And the Object

The three property

*.__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

Deep dive into the parent

> 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

Remove the progress

const func = Object.__proto__
const obj = Object.prototype
const a = new Object()

Conclusion

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.

Pesudo code

class obj {
    constructor() [[native code]]
}
class func extends obj {
    constructor() [[native code]]
}
const Object = obj.constructor;
const Function = func.constructor;

What is class in JavaScript?

> typeof (class A {})
'function'

The class declaration creates a new class with a given name using prototype-based inheritance.

Object instanceof Function is true

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;

Object instanceof Object is true

func inherits from Object.prototype aka. obj;  
obj is an object;  
Object inherits from obj;  

Function instance of Object is true

func is an object;
Function inherits from func;
func inherits from obj.

Function instanceof Function is true

Function is an instance of itself (naturally, since it’s a function, and thus an instance of Function).

The prototype chain looks like this

Object   ---> Function.prototype ---> Object.prototype ---> null
Function ---> Function.prototype ---> Object.prototype ---> null

Back to the definition, from the MDN

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.

Future reading

Inheritance and the prototype chain, MDN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment