1. Each function we create has a prototype attribute, which points to an object, that is, a prototype object. Prototype objects contain attributes and methods shared by all instances of this particular type, so prototype objects can be understood as instances of this particular type constructor.
function Person() { this.name="Lily", this.age= 18 this.sayName = function() { console.log(this.name) } } console.log("Person.prototype", Person.prototype); console.log("Person.prototype._proto_", Object.getPrototypeOf(Person.prototype));
2. The object created by JS has a _proto_attribute, which connects the prototype object of instance and constructor. It is invisible to the outside world and can not be obtained directly. This attribute can be obtained by Object.getPrototypeOf () method.
var person1 = new Person(); console.log(person1)
3. The prototype of all constructors is object type, but the prototype of function is an empty function, and the _proto_of all constructors (built-in objects) points to this empty function.
console.log(typeof Person.prototype)// object console.log(typeof Object.getPrototypeOf(person1))// object console.log(Function.prototype) // f(){} console.log(typeof Function.prototype)// Function console.log(typeof Object.prototype)// object console.log(typeof Array.prototype)// object console.log(typeof Number.prototype)// object console.log(typeof Date.prototype)// object console.log(typeof String.prototype)// object console.log(typeof Boolean.prototype)// object console.log(Object.getPrototypeOf(Boolean))// f(){}
4. When the code acquires the attributes of an object, it first looks up the object instance itself, and if it cannot find it, it searches up its prototype object. Therefore, changing the attribute value in the prototype object in the instance does not override the attribute value, only the attribute value in the prototype object is shielded. When an attribute is added to an instance, it masks the same-name attribute in the prototype object. If you want to access the attribute value in the prototype object, you need to use delete to completely delete the attribute with the same name in the instance. (Note: Using delete to delete the attributes in the constructor will delete the attributes in the prototype object of the instance, that is, if the name is placed in Person(), the name of person2 will not exist with delete.)
function Person() {}; Person.prototype.name = 'Lily'; Person.prototype.age = 18; var person1 = new Person(); var person2 = new Person(); person2.name = "Tom"; console.log(person1.name);// Lily console.log(person2.name); // Tom delete person2.name; console.log(person1.name); // Lily console.log(person2.name); // Lily
5.hasOwnProperty("Property Name") can detect whether an attribute exists in an instance or a prototype object, and its return value true represents the existence of an instance, and false represents the existence of a prototype object.
6. The in operator returns true when an attribute can be accessed (either in an instance or in a prototype object), so when the in operator returns true and hasOwnProperty () returns false, it can be judged that the attribute exists in the prototype object rather than in the instance.
7. When an object literal is used to rewrite a prototype object, its constructor property is changed to point to the Object constructor instead of the original object.