problem
Js beginner students, always get the prototype js what it is, looking at the console print out a string of __proto__, into confusion.
For example, I define a Person, create an instance p, and print examples.
function Person(){}
var p = new Person();
console.log(p)
Drawing, print out a Person instance of an object p,
this object has a __proto__ property, what is this thing?
Under __proto__ property and another constructor property __proto__ property.
What constructor is why the result is printed Person ()?
What other __proto__ that?
In order to answer questions on top of, we need to understand some of the concepts.
A prototype
prototype
First of all, explain, JS, the things are objects.
Each object has a property of the prototype function (function object-specific attribute), this property is a reference to an object, the object is to effect the properties and methods shared by all instances. We put this object is called the prototype object, also known as explicit prototype .
Each object has a property of the __proto__, may also be referred to as implicit prototype , the prototype object points implicit prototype to create the object constructor function (prototype).
constructor
Prototype object constructor function has a property, which is a reference point for the original constructor.
relationship
How we understand the concept of the top, and what is the connection between them?
We may wish to print out p .__ proto__ property:
you can see, print out an object, the object inside has a property constructor.
What constructor is it, print the following:
you can see, print it out is a function that is representative of the Person constructor itself.
In turn printed prototype Person.prototype Person constructor
and its prototype property constructor:
From the graph we can be drawn:
1. Examples of the object p points have attributes __proto__ prototype object is to create Person.prototype its constructor.
2. prototype object constructor Person.prototype a Person attributes constructor constructor itself.
We can verify:
However, they might have confused some students, that since all objects have __proto__ properties
that the constructor Person () property __proto__ who directed it?
Of course, it is a pointer to the prototype object to the constructor.
The constructor is the Function (), thus pointing __proto__ herein Function.prototype.
That prototype object is an object whose attributes __proto__ who directed it?
Similarly, it points to the prototype object constructor, that Object.prototype.
Here, we have to mention that the concept of the prototype chain.
Prototype chain
Prototype is a chain mechanism, referring js, each object has a property the __proto__, pointing to its prototype object constructor. Prototype object is an object, therefore also directed __proto__ property prototype object prototype object, so that a layer upward until the object is empty prototype object (Object prototype object of __proto__ Object.prototpye attribute to null).
Thus, the relationship between the example prototype chain follows:
P .__ proto__ point Person.prototype,
Person.prototype .__ proto__ is directed Object.prototype,
Object.prototpye null point .__ proto__
In order to understand the relationship between the top example of the prototype chain, we draw a picture to increase understanding:
So far, the problem is not the top of the heart has the answer yet.
to sum up
1. Each object has a __proto__ property, it points to create a prototype object constructor.
Role: constitute the prototype chain used to implement prototype-based inheritance.
2. In addition to the function __proto__ property, there is a prototype property for prototype object pointers to functions.
Role: for sharing prototype-based inheritance and property.
So, you would have seen in many places like this wording:
function Person(){}
Person.prototype.hello = function(){
console.log("hello")
}
The code represents the top, all instances of Person objects created can be shared hello method.
3. The prototype object constructor have attributes constructor, the constructor point itself.
PS: No welcome public attention: "Should the breeze" with the exchange of learning.