Prototype: JavaScript reference type
What is prototype?
The prototype attribute in the Javascript Object can return the prototype reference of the object type, and the prototype belongs to the constructor.
In JavaScript, each object is connected to a prototype object and can inherit attributes from it. All objects created by Object literal are connected to Object. prototype, which is a standard Object in JavaScript.
When creating a new object, you can select an object as its prototype. The new object has all attributes and methods of the prototype object. If the new object has a property or method with the same name as the original object, the new object overwrites the original object.
function Cat(){this.name = "tomcat";this.age = 3;this.salary = 123;this.sayName = function(){alert(this.name);}}function CatSon(){this.name = "dingdang";this.address = "new york";this.father = "tomcat";this.hobby = "fishing";};CatSon.prototype = new Cat;var catSon = new CatSon;alert(catSon.age); // output 3alert(catSon.father); // output tomcatcatSon.sayName(); // output dingdang
In the code above, we define two constructor Methods Cat () and CatSon (), where CatSon. prototype points to new Cat; that is, if the prototype of CatSon points to a Cat instance, The CatSon instance has all the attributes and methods of the Cat instance.
Because CatSon also defines the name attribute, the name attribute value of CatSon is dingdang, that is, the sub-instance attribute with the same name overwrites the prototype attribute.
When we change an object, the prototype of the object is not touched.
After connecting the code, we modify catSon. name.
// Modifications to the object will not touch the prototype catSon. name = "jiafei"; alert (CatSon. prototype. name); // output tomcat
Delegate
The prototype connection is used only when the attribute value is accessed. If we try to get a property value of an object, but this object does not have this property name, JavaScript will try to get the property value from the prototype object. If the prototype Object does not have this attribute, search for it from its prototype, and so on until the process reaches the end Object. prototype. If the desired attribute does not exist in the prototype chain, the result is the undefined value. This process is calledDelegate.
In the code above, we access the catSon. door attribute.
Because CatSon does not define the door attribute, Cat, the prototype of CatSon, does not define this attribute, and does not define this attribute in the prototype Object of Cat. Therefore, the value of catDoor is undefined.
alert(catSon.door); // output undefined
A prototype relationship is a dynamic relationship. If we add a new property to the prototype, the property will be immediately visible to all objects created based on the prototype.
// The prototype is dynamically bound to Object. prototype. door = "blueDoor"; alert (catSon. door); // output blueDoor
Methods related to prototype
Next, let's look at several prototype-related methods in the Object class.
HasOwnProperty
Determines whether an object has a specific attribute.
This attribute must be the property of the object and cannot be the property or method inherited from the prototype.
alert(catSon.hasOwnProperty("name")); // truealert(catSon.hasOwnProperty("sayName")); // false
IsPrototypeOf (object)
Determine whether the object is a prototype of another object
alert(Cat.prototype.isPrototypeOf(catSon));// truealert(CatSon.prototype.isPrototypeOf(catSon));// true var obj = new Object;alert(CatSon.prototype.isPrototypeOf(obj));// false
PropertyIsEnumerable (property)
Determine whether a given property can use... Enumerate in statements
alert(catSon.propertyIsEnumerable("name")); // truefor (pro in catSon){alert(pro + "=" + catSon[pro]);}