js in the understanding of the constructor and prototype


1.①__proto__ and the object constructor property is unique; ② prototype property is a unique function, because function is also an object, so also has the function __proto__ and constructor property.

2 .__ role proto__ property is that when accessing properties of an object, if the object is inside this property does not exist, then it will go to that object __proto__ attribute points (parent) where to find, been looking for, null __proto__ property until the end and then returns undefined, the objects connected by this link __proto__ property which we called prototype chain.

3.prototype role is to make the property function instantiated object who can find common properties and methods that a1 .__ proto__ === A.prototype.

4.constructor meaning attribute is a pointer to the object's constructor function, all functions (in this case as the subject) constructor final point Function ().

function A() {}
A.prototype.name = '1';
where a1 = new A ();
There a2 = new A ();

console.log(a1.name); // 1
console.log(a2.name); // 1

a1.__proto__.name = '2';
a1.name = 3;
// console.log(a1.__proto__);

console.log(a1.name);   // 3
console.log(a2.name);   // 2

delete a1.name;

a2.__proto__.name = '4';
console.log(a1.name);   // 4
console.log(A.prototype.constructor);  // A

  

Guess you like

Origin www.cnblogs.com/robinw666/p/11787409.html