JavaScript and prototype study notes --__ proto__

_ _ Proto_ _ and prototype

First remember these two knowledge points

__proto__Object-specific attributes; prototypeis a function of specific attributes.

Function is a special object, the function of both prototypeproperty also has __proto__properties.

1.__proto__

Create an empty object and print

var a = new Object();
console.log(a);

To give the following output

A display output have an attribute __proto__, which is each object has attributes, see __proto__contains many properties.

__proto__Pointing to the 创建该对象的构造函数prototype.

Because of this property, even when the object did not do anything, you can also call toString (), valueOf () methods.

He says it is easy to understand, at the time the child had just been born, there is some common sense.

__proto__The role of property is that when accessing properties of an object, if the object is inside this property does not exist, then it will go to __proto__that object attribute points (parent) where to find, been looking until __proto__the properties of the end null, then look up the equivalent value in null, it will complain. By __proto__the properties of objects together this link that is what we call the prototype chain .

2.prototype

Following the above code to continue printing

console.log(Object.prototype);

We can see Object.prototype and a .__ proto__ exactly the same

Verify and continue printing

console.log(a.__proto__===Object.prototype);

The result is true

Is this a coincidence? Let's look at another example

function Person() {};
p = new Person();

console.log(p);
console.log(Person.prototype);
console.log(p.__proto__=== Person.prototype);

In fact, this is a new masterpiece in the creation of new analysis function in the end what has been done, it can be split into three steps

(1) var a = {}; that is, initializes an object p;

(2) p. __ proto __ = Person.prototype;

(3) Person.call (p); that is configured to p, p may be called to initialize

That the role of the prototype property, what is it? Its role is to include all the properties and methods can be made to share examples of a particular type, that is, let the function instantiated object who can find common properties and methods.

 

Guess you like

Origin www.cnblogs.com/tanyx/p/12162137.html