The prototype javascript and understanding __proto__

Sometimes see at work and prototype __proto__ these two properties, these two attributes I have been more Mongolian laps, but by me access to relevant information, decided to make a few concluding deepen their understanding, written in the wrong place also please the god said.

Two methods associated with property __proto__

Analyzing the object instance attribute is present, the process of the prototype object is present

Several methods of obtaining or traverse the object attributes

1、prototype

Each function has a prototype property, this property is a pointer to an object. And the use of the object properties and methods are shared by all instances of a particular type. The benefits of using this object is that it allows all instances of shared objects it owns the properties and methods

2、 __proto__

Each instance of an object has a property __proto__ for prototype object point constructor. __proto__ property at the time of calling the constructor creates an instance of an object produced.

function Person(name, age, job){

this.name = name;

this.age = age;

this.job = job;

this.sayName = function(){

console.log(this.name);

}; // declare the functions are logically equivalent

}

var person1=new Person("Nicholas",29,"Software Engineer");

console.log(person1);

console.log(Person);

console.log(person1.prototype);//undefined

console.log(person1.__proto__);

console.log(Person.prototype);

console.log(person1.__proto__===Person.prototype);//true

Output:

to sum up:

1, prototype attribute instance of an object calling the constructor created for "undefined", prototype constructor is an object.

2, __ proto__ property when creating an object instance generated by calling the constructor.

3, __proto__ attribute instance of an object created by calling the constructor prototype constructor points.

4, by default, all automatically obtain a prototype object constructor (the constructor) property, which contains a pointer to where the pointer to the function prototype property.

The following figure shows the relationship between the various objects After creating the instance constructor Person

The figure shows the constructor Person, Person relationship between the properties of the prototype and conventional two Person instance.

3, two methods associated with property __proto__

isPrototypeOf (): Although not access to __proto__ in all implementations, but it can be determined whether there is a relationship between objects by isPrototypeOf () method.

alert(Person.prototype.isPrototypeOf(person1)); //true

alert(Person.prototype.isPrototypeOf(person2)); //true

Object.getPrototypeOf (): supported in all implementations, this method returns a value of __proto__. E.g:

alert(Object.getPrototypeOf(person1) == Person.prototype); //true

alert(Object.getPrototypeOf(person1).name); //"Nicholas"

Note: While the stored values ​​can be accessed through the object in the prototype example, but not by the value of the prototype object instance rewrite. If we add an attribute in the instance, and the property is a property of the same name with the instance of the prototype, then we create the property in the example, the property will shield the properties in the prototype. Consider the following example:

function Person(){

}

Person.prototype.name = "Nicholas";

Person.prototype.age = 29;

Person.prototype.job = "Software Engineer";

Person.prototype.sayName = function(){

alert(this.name);

};

was PERSON1 = new Person ();

was person2 = new Person ();

person1.name = "Greg";

alert (person1.name); // "Greg" - from Example

alert (person2.name); // "Nicholas" - from prototype

4, the attribute is determined in the presence of an object instance, or in the presence of the prototype object, the following method

hasOwnProperty (): can detect a property is present in the example, still present in the prototype. Return value of true indicates that the object instance attribute is present, other conditions are false.

in operator: regardless of whether the property is present in the prototype example. As long as there is an object, it returns true. May be used simultaneously hasOwnProperty () method, and in the operator, it may be determined in the end that the property is present in the subject or is present in the prototype.

was PERSON1 = new Person ();

was person2 = new Person ();

alert(person1.hasOwnProperty("name")); //false

alert("name" in person1); //true

person1.name = "Greg";

alert (person1.name); // "Greg" - from Example

alert(person1.hasOwnProperty("name")); //true

alert("name" in person1); //true

alert (person2.name); // "Nicholas" - from prototype

alert(person2.hasOwnProperty("name")); //false

alert("name" in person2); //true

delete person1.name;

alert (person1.name); // "Nicholas" - from prototype

alert(person1.hasOwnProperty("name")); //false

alert("name" in person1); //true

5, several methods of obtaining or traverse the object attributes

for-in: for-in through the return cycle is able to be accessed, it may be enumerated property, regardless of whether the property is in the example, the presence or prototype.

function Person(name, age, job) {

this.name = name;

this.age = age;

this.job = job;

}

Person.prototype={

sayName:function(){

return this.name;

}

}

var p = new Person ( "Li", 30, "poet");

for(var prop in p){

console.log(prop);//name、age、job、sayName

}

console.log(Object.keys(p));//["name", "age", "job"]

console.log(Object.keys(Person.prototype));//["sayName"]

console.log(Object.getOwnPropertyNames(Person.prototype))

// ["constructor", "sayName"]

Object.keys (): get all enumerable properties on an object instance. Object.getOwnPropertyNames ():  Gets all the properties of the object instance, whether it is enumerable.

Note: When using an object literal to rewrite the entire prototype object, in essence, a complete rewrite of the default prototype object, therefore constructor property will become property of the new object's constructor (pointing to the Object constructor), no longer point to Person. But by specifying constructor property when rewriting the prototype object to make still point to the original constructor. At this time, although instanceof operator can return the correct results, but has been unable to determine the type of the object by the constructor.

object instanceof constructor: detecting constructor.prototype prototype chain exists on the object parameters.

function Person() {}

var friend2 = new Person();

Person.prototype = {

//constructor : Person,

name: "Nicholas",

age: 29,

job: "Software Engineer",

sayName: function() {

alert(this.name);

}

};

var friend = new Person();

console.log(friend2 instanceof Object); //true

console.log(friend2 instanceof Person); //false,

console.log(friend2.constructor == Person); //true

console.log(friend2.constructor == Object); //false

console.log(friend instanceof Object); //true

console.log(friend instanceof Person); //true

console.log(friend.constructor == Person); //false

console.log(friend.constructor == Object); //true

Due to the dynamic nature of the prototype, is an example will add a pointer pointing to the first prototypes Prototype when calling the constructor, and to modify the prototype is equivalent to another object cuts off communication between the constructor and the initial prototype. See the examples below

function Person(){

}

var friend = new Person();

Person.prototype = {

constructor: Person,

name : "Nicholas",

age : 29,

job : "Software Engineer",

sayName : function () {

alert(this.name);

}

};

var friend2=new Person();

friend.sayName(); //Uncaught TypeError: friend.sayName is not a function

friend2.sayName();//Nicholas

console.log(friend instanceof Person);//false

console.log(friend instanceof Object);//true

console.log(friend2 instanceof Person);//true

Result Analysis: This is because friend1 the prototype points to Person.prototype not before rewriting Person.prototype, that is, the initial prototype object constructor. The friend2 the prototype points to Person.prototype rewritten Person.prototype. As shown below

13696553-8cb1c7e6403b264e.png

6, the prototype chain

The basic idea is to use the prototype to make a reference type inherits from another type of reference properties and methods. The most intuitive performance is to make prototype object is equal to another instance of the type.

function SuperType(){

this.property = true;

}

SuperType.prototype.getSuperValue = function(){

return this.property;

};

function SubType(){

this.subproperty = false;

}

// inherited SuperType

SubType.prototype = new SuperType();

SubType.prototype.getSubValue = function (){

return this.subproperty;

};

var instance = new SubType();

alert(instance.getSuperValue()); //true

SubType.prototype = new SuperType (); This code such that all properties and methods to the examples Supertype originally present in, now present in the SubType.prototype. It makes the instance of the constructor points to SuperType.

console.log(instance.constructor===SuperType);//true

Summary: When accessing an instance property, the property will first search in the instance. If the property is not found, the search will continue instance of the prototype. In the case of inheritance through prototype chain, the search process is to continue up the prototype chain. In the case of a property or method can not be found, the search process is always forward of a ring to the prototype chain will stop.

Take the above example, calling instance.getSuperValue () will search through four steps:

Search instance instance;

Search SubType.prototype;

Examples of search SuperType;

Search SuperType.prototype, the final step will be to find the method.

You may also be interested in the article:

Detailed Java Basics - Object Oriented 1 (constructors, static, this keyword)

Detailed Java variables and constants

On the Java parameter passing problem

vue + element + Java achieve bulk delete function

On the output format in Java

On the Java numeric type conversion and coercion

12 military regulations summary Java exception handling

Java Property class uses Detailed

Java, automatic packing, unpacking caused by consuming Detailed

Article simultaneous release:  https://www.geek-share.com/detail/2769998109.html

Reproduced in: https: //www.jianshu.com/p/5639823b558d

Guess you like

Origin blog.csdn.net/weixin_33860528/article/details/91184318