, every function we create has a prototype (prototype) property. This property is a pointer, pointing to a prototype object, and the attributes and methods possessed in this prototype object can be shared by all instances.

 function (Person) {Person.prototype.name} = "Nicholas"; Person.prototype.age = 29; Person.prototype.sayName = function (ALERT) {(this.name)}; VaR; person1 = new (Person); person1.sayName ("Nicholas"); / / var person2 = new (Person); person2.sayName ("Nicholas"); / / Alert (person1.sayName = = person2.sayName); //true

, a

prototype object to understand no matter what time, as long as you create a new function, can according to a specific set of rules for this function to create a prototype attribute, this attribute points to the prototype object function.

, by default, all prototyping objects automatically get a constructor (constructor) attribute, which contains a pointer to the function of prototype attribute.

, when calling a constructor to create a new instance, the inside of the instance will contain a pointer (internal attribute), pointing to the prototype object of the constructor. The pointer to the tube in the ECMA-262 fifth version is called [[Prototype]]. Although

has no standard way to access [[Prototype]] in script, Firefox, Safari and Chrome support one attribute __proto__ on every object. In other implementations, this attribute is completely invisible to script.

, however, the real important point is that the connection exists between the instance object and the prototype object of the constructor, rather than between the instance and the constructor.

is an example of the previous code that uses the Person constructor and Person.prototype to create an instance, and figure 6-1 shows the relationships among the objects.


" here, Person.prototype points to the prototype object, and Person.prototype.constructor also refers to Person.

person1 and person2 contain an internal attribute, which only points to Person.prototype; in other words, they have no direct relationship with constructors.

can call person1.sayName (). This is achieved by finding the process of the object property. (you will search for an instance first, and if you don't find it, you will continue to search the prototype.)

alert) with isPrototypeOf method to determine the relationship between the instance and prototype object (Person.prototype.isPrototypeOf (person1) //true (Person.prototype.isPrototypeOf); alert (person2)); //true

(Object.getPrototypeOf) method with the prototype object
alert returns the instance (Object.getPrototypeOf (person1) = = Person.prototype); //true

hasOwnProperty () method can detect a the attribute is present in the case, still exist in the prototype.
alert (person1.hasOwnProperty ("name"); //false) a prototype
person1.name = "Greg";
alert (person1.name); / / "Greg" -- from the example of
alert (person1.hasOwnProperty ("name"); //true


two, more simple prototype syntax

example in front of each add an attribute and method to knock over Person.prototype. In order to reduce unnecessary input and to visually encapsulate prototype functions, a more common way is to rewrite the entire prototyping object with a literal volume containing all attributes and methods.

 function (Person) {Person.prototype} = {Name: Nicholas, age: 29, job: "Software Engineer", sayName: function (ALERT) {(this.name)};}; 

in the code above, we set Person.prototype to equal a new object is created with object literals form. The final result is the same, but there is an exception: the constructor attribute no longer points to the Person.

has introduced before that every time a function is created, it will create its prototype object at the same time, and the object will also get the constructor property automatically.

 var friend (Person) = new; alert (friend instanceof Object); //true alert (friend instanceof Person); //true alert (friend.constructor = = Person); //false alert (friend.constructor = = Object); //true

is here, with the instanceof operator to test Object and Person still return to true, but the constructor attribute is not equal to Object equal to Person.

if the value of the constructor is really important, you can set it back to the appropriate value as the following.

 function (Person) {Person.prototype} = {constructor: Person, name: Nicholas, age: 29, job: "Software Engineer", sayName: function (ALERT) {(this.name)};}; 

, three native object prototype

all primary reference types (Object, Array, String, etc.) in the constructor of the prototype defines methods.

, for example, can find the sort () method in Array.prototype, and the substring () method can be found in String.prototype. Although it can be done, it is not recommended to modify the prototypes of native objects. The

four, the problem of the prototype object of the prototype object, the biggest problem of the

prototype pattern, is caused by its shared nature. One of them is modified, and the other will be affected.

 function (Person) {} Person.prototype {constructor: = Person, name: Nicholas, age: 29, job: "Software Engineer friends": "Shelby", "Court"], sayName: function (ALERT) {(this.name)};}; VAR person1 = new (Person) VaR; person2 = new (Person); person1.friends.push ("Van"); alert (person1.friends); / / Shelby, Court, Van "alert (person2.friends); / / Shelby, Court, Van" alert (person1.friends = person2.friends); the basic idea of //true


is the prototype of the prototype chain let a reference type inherits from another type of attribute references and methods. The basic concept of the so-called prototype chain is made up of the chain of example and prototype.

 function (SuperType) {this.property = true; SuperType.prototype.getSuperValue = function)} ({return}; SubType (this.property; function) {this.subproperty = false;} / / SubType.prototype = new SuperType inherited SuperType (); SubType.prototype.getSubValue = function (return) {this.subproperty};; VAR instance = new SubType (ALERT); (instance.getSuperValue) (//true

); Picture Description:


property is located in SubType.prototype. This is because property is an instance attribute, and getSuperValue () is a prototype method. Since SubType.prototype is now an instance of SuperType, then property is of course in the instance.

you might be interested in this article:

to understand the prototype chain ""

This concludes the body part

This paper fixed link:http://www.script-home.com/analysis-of-javascript-archetype-and-prototype-chain.html | Script Home | +Copy Link

Article reprint please specify:Analysis of JavaScript archetype and prototype chain | Script Home

You may also be interested in these articles!