As a front-end developers, we all know that JS is single inheritance, which is the top Object.prototype prototype chain includes all objects inherited toString (), valueOf () and so on public property from it.
The origin of the chicken and egg problem
First, Object
and Function
are constructors and constructors are all Function
examples of objects. So Object
is Function
the instance of the object; and Function.prototype
is Object
an instance of an object. So here's an interesting corollary question of the chicken and the egg:
Object instanceof Function // true
Function instanceof Object // true
Object.__proto__ === Function.prototype // true
Function.__proto__ === Function.prototype // true
Function.prototype.__proto__ === Object.prototype // true
复制代码
Well Object
and Function
who is chicken egg who is it?
Then on to delve into chicken eggs under the above code caused by the chicken issue, this starts with the following prototype / prototype chain classic map, in-depth understanding of this process Object.prototype
, Function.prototype
, function Object()
, function Function()
the relationship between this process may be a bit burned brain, after all, is a big thing JS metaphysics.
Object.prototype
The end of the prototype chain is Object.prototype
(without regard to null
the case of). All objects are from Object.prototype
public property inherited toString () and so on .
Object.prototype
It represents the Object
prototype object, in fact, Object.prototype
not by the Object
function of creation, why? Look at the following code:
function Dog() {
this.name = '川普';
}
var dog = new Dog();
dog.__proto__ === Dog.prototype; // true
复制代码
Examples of objects __proto__
will point to the constructor prototype
that dog.__proto__
point Dog.prototype
, but Object.prototype.__proto__
again null
, it is Object.prototype
not through Object
functions created, how it was produced? In fact, Object.prototype
the browser according to an object underlying the creation of the ECMAScript specification, so just look at the classic view of the inside Object.prototype
by Object
the function of creation, not really.
Function.prototype
Function.prototype
And Function.__proto__
for the same object .
This also means that: Object
/ Array
etc on the structure and function is essentially Function
the same, are inherited Function.prototype
from the classic map view is through new Function
structure out .
Of course, Function.prototype
the object is a function (object), which __proto__
attribute points Object.prototype
, i.e. Function.prototype
directly inherited the root Object.prototype
( ).
By this point we can figure out the inherited prototype chain : Function|Object|Array...--->Function.prototype--->Object.prototype(root)
. As shown below:
function Object()
Object
As a constructor and its __proto__
attribute points Function.prototype
, namely:
Object.__proto__ === Function.prototype // true
复制代码
From the classic map view:
Used new Object()
when creating instance objects o1, o1 of the object instance __proto__
attribute points constructor prototype
attribute corresponding figure it is Object.prototype
that o1.__proto__ === Object.prototype
the result is true
.
Function.prototype
Pointing to objects, it __proto__
will point to Object.prototype
, because Function.prototype
the object pointed to is also a common Object
object creation, so it follows the basic rules.
function Function()
Function
Is also a function object, but also __proto__
property, since it is a function, then it must have been Function
created, it Function
is self-created, so it's __proto__
pointing to its own Prototype
:
Function.__proto__ === Function.prototype // true
复制代码
Here a little brain burn it, we look at chicken-and-egg problem.
Function & Object chicken and egg problem
As is clear from the above, Object
the constructor inherited Function.prototype
, while Function
the constructor inherited Object.prototype
, there arises a chicken and egg problem. Why is there such a problem? We must first understand a deeper level to Function.prototype
this object because it is the cause Function instanceof Object
and Object instanceof Function
are true
the reason.
// Object instanceof Function 即
Object.__proto__ === Function.prototype // true
// Function instanceof Object 即
Function.__proto__.__proto__ === Object.prototype // true
// Object instanceof Object 即
Object.__proto__.__proto__ === Object.prototype // true
// Function instanceof Function 即
Function.__proto__ === Function.prototype // true
复制代码
The JS specification, Function.prototype
but is different from the general function (object) function (object), wherein:
Function.prototype
Like normal function can be called, but always returnedundefined
.- Normal function is actually
Function
an instance of a normal function that is inheritedFunction.prototype
. Iefunc.__proto__ === Function.prototype
. Function.prototype
InheritedObject.prototype
, and notprototype
the property.- So,
Function.prototype
in fact, is a different type of function, it can be independent of / prior toFunction
production.
And Object
itself is a (structural) function, are Function
examples, i.e., Object.__proto__
it is Function.prototype
.
Summary: prior Object.prototype
(prototype chain to the top), Function.prototype
succession Object.prototype
to produce, finally, Function
and Object
other inherited constructors Function.prototype
generated .
I guess we see here are tired of, is not it still a bit confusing? Chaos is normal. That article would let it mess with, next we will invite another friend to help, it completely clear rationale, old friend is - , instanceof
then please listen to next time decomposition slightly.
If you think the article to help you slightly, welcome in my GitHub blog points praise and attention, very grateful!