Meaning of the prototype: if the constructor has a prototype object a, the instances created by the constructor must be copied to a. A prototype is also an object that can be acquired and changed at run time. You can add attributes to the prototype or delete properties that already exist on the prototype . The prototype object is used to implement inheritance.
The 2.prototype attribute, which is the property of each function, (all types of constructors are also functions), prototype is a property containing the constructor attribute. object, where the constructor property holds A reference to the function.
the properties on the 3.prototype can be shared by all instances because each instance has a _proto_ property.
4 prototype chain: all objects in Js ( except Undefined,null) have a built -in [[prototype]] (_proto_) attribute pointing to its "parent class" prototype. put this there . _proto_ strung up until the Object.prototype.proto to be NULL chain is called the prototype chain. .
5.function Fun () {
Code
}
The following actions are performed inside this function:
Add a prototype (or prototype) property to the function, adding an additional constructor property to the prototype object . And this property holds a reference to the function Fun () .
var obj = new Fun (); when you use a constructor to create an instance object, an instance of the object automatically saves a pointer to its constructor. prototype an attribute of the object _proto_, so in each of our object instances, we can access the constructor's prototype all the properties and methods.
The following example illustrates the role of prototypes and the embodiment of the prototype chain. :
function createperson (name,age,job) { this.name = name; this.age = age; this.job = job; } createperson.prototype.firstname = "XI"; createperson.prototype.getname = function () { alert (this.name); }; var per1 = new createperson ("Xixi", "SE"); var per2 = new createperson ("Xiyin", "F", "FF");
Createperson . Prototype sets the properties of a function object firstname, and a method GetName (), that Createperson The normal objects that come out of an instance (in the example:per1,per2) inherit these properties and methods, which is the role of prototypes.
Use a diagram to illustrate the whole process, as shown below:
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/80/2C/wKioL1c6jtmAGsQMAAF_4pYfGiM784.jpg "title=" 1.jpg " alt= "Wkiol1c6jtmagsqmaaf_4pyfgim784.jpg"/>
This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1774258
JS Zhongyuan type, learning and understanding of prototype chain