1. Introduction
-
proto (__ proto__)
In similar
java
language, the concept of inheritance is passed between classes and to achieve, butjavascript
there is no class, it is an object, therefore, injavascript
, the concept of inheritance is passed between the object and the object implementation.In consideration of
javascript
succession when the scope is limited to reference data types, reference data types, although dividedFunction
, andObject
two kinds, but in matters of inheritance, without distinctionFunction
andObject
, just as objects can be unified.__proto__
Andprototype
different, prototype only in Function there, and__proto__
thenFunction
andObject
in both. JavaScript is used__proto__
to determine the inheritance relationship.javascript
The essence of inheritance:__proto__
Object B pointed to by the properties of an object A. B is the prototype object (or parent object) of A. Object A can use the properties and methods of object B, and can also use the prototype object C of object B. The properties and methods on the above, recursively, are the so-called prototype chain.var A = { name:'前端收割机'} var B = { age:18} var c = { hobby:'摄影'} A.__proto__ = B;//将B设置为A的父对象 B.__proto__ = C;//将C设置为B的父对象 console.log(A.name) //前端收割机 console.log(A.age) //18 console.log(A.hobby)//摄影
-
prototype
One
__proto__
realizes the inheritance problem. That shouldprototype
do it?prototype
What role does underway in succession, in fact,prototype
the real work is at theFunction
time as a constructor, because__proto__
is not the official standard defined attributes, so helpprototype
attribute to mimic the inheritance patterns between class and class.When a
new
function is called by a keyword, the constructor is executed, which is responsible for creating an instance object,__proto__
pointing the properties of the instance object to the constructorprototype
to implement inheritanceprototype
of all the properties and methods of the constructor,this
binding it to the instance, and then Execute the function body.From the above figure, we can find the prototype chain of
foo
objects andFoo
functions:1.
foo
Object prototype chainfoo.__proto__ == Foo.prototype; foo.__proto__.__proto__ == Foo.prototype.__proto__ == Object.prototype; foo.__proto__.__proto__.__proto__ == Foo.prototype.__proto__.__proto__ == Object.prototype.__proto__ == null;
2.
Foo
Constructor prototype chainFoo.__proto__ == Function.prototype; Foo.__proto__.__proto__ == Function.prototype.__proto__; Foo.__proto__.__proto__.__proto__ == Function.prototype.__proto__.__proto__ == Object.prototype.__proto__ == null;
The constructor
Foo
is not on the prototype chainFoo.prototype
, soFoo.prototype
the properties and methods cannot be inherited .foo
The prototype chain of the instance has itFoo.prototype
, so itfoo
can inheritFoo.prototype
the properties and methods.
Second, the role
-
proto (__ proto__):
- Realize the inheritance between objects and form a prototype chain.
- Make the child object can use the properties and methods of the parent.
-
prototype:
- Let the objects instantiated by the constructor find common attributes and methods (ie
foo.__proto__ === Foo.prototype
). - The
Javascript
means ofprototype
property to mimic the inheritance patterns between the implementation class and class.
- Let the objects instantiated by the constructor find common attributes and methods (ie
Three, summary
- proto
(和
constructor属性)是**对象**所独有的;prototype
attribute is a function of unique, because the function is also an object, so also has the function__proto__
andconstructor
properties. __proto__
The function of the attribute is when the attribute of an object is accessed, if the attribute does not exist in the object, it will__proto__
search for the object (parent object) pointed to by its attribute, and keep searching until__proto__
the end of the attributenull
. Make the child object can use the properties and methods of the parent.prototype
Properties of the role is to make the function objects instantiated who can find common properties and methods, theJavascript
means ofprototype
property to mimic the inheritance patterns between the implementation class and class.