The following sample code by section described the basic concepts of the prototype model and knowledge.
<! DOCTYPE HTML> <HTML> <head> <title> prototype model </ title> <Script type = "text / JavaScript"> function the Person () {}; // Person.prototype.name = "Lucy"; / / Person.prototype.age = 15; // Person.prototype.sayName = function () { // Alert (this.name); // }; // 3. more simple prototype syntax Person.prototype = { constructor: the Person, // if the constructor property value is really important, you need to manually set back to the appropriate value. name: "Lucy" , Age: 15 , sayName: function () { Alert ( the this .name); } }; // var P1, P2; P1 = new new the Person (); P2 = new new the Person (); // hasOwnProperty () detects a presence in the instance attribute (true), or is present prototype (to false) Alert (p1.hasOwnProperty ( "name")); // to false p1.name = "Bob" ; p1.job = "Student"; // set the attribute instance Alert (p1.name); // "Bob", from example Alert (p1.hasOwnProperty ( "name")); // to true Alert (p1.sayName == p2.sayName);//Examples of methods to check whether the true shared Alert (p1.job); Alert (p1.hasOwnProperty ( "Age")); // to false Alert (p1.hasOwnProperty ( "Job")); // to true // / ********** ************************* 2. prototype and in operator ****************** *************************** / // / ******** alone in operator ****** ** / // when the object is able to access through access to the property, the return to true Alert ( "the Job" in p1); // to true Alert ( "Age" in p1); // to true Alert ( "name" in p1); / / to true Alert ( "name" in P2);//true // This function determines property is still present in the prototype object present in the object instance. true: the prototype object property attribute; false: an instance of the object properties. function hasPrototypeProperty (Object, name) { return object.hasOwnProperty (name) && (name! in Object); } Alert (hasPrototypeProperty (P1, "Job" )); Alert (hasPrototypeProperty (P2, "name" )); // / *********************************************************** / // / ******** for in-**** ******* / // returns all can be accessed through the object, can be enumerated attributes, including attributes present in the prototype, but also present in the instance attributes. // Object.keys (Object): receiving object as a parameter and returns a string array containing all enumerable properties. var Keys =Object.keys (Person.prototype); Alert (Keys); // "name, Age, sayName" var p1keys = Object.keys (P1); Alert (p1keys); // "name, Job" // sequence is the for-in the order of access for ( var prop in p1keys) { Alert (p1keys [prop]); } // Object.getOwnPropertyNames (Object) Gets all instances of attributes, including not enumerated attribute var p1allkeys = Object.getOwnPropertyNames (P1 ); Alert (p1allkeys); // "name, Job" var p1allkeys = Object.getOwnPropertyNames (Person.prototype); Alert (p1allkeys); // constructor, name, Age, Job, sayName constructor property is not enumerable // / *************************************************** ***** / // / ***************************************** **************************** / / * *********************************** 3. more simple prototype syntax ******* ******************************** * / // set it to the simple prototype syntax, Person.prototype set a the new object is created as an object literal form. The final results are identical, but the object's constructor property is no longer pointing to Person. // If the constructor property value is really important, you need to manually set back to the appropriate value. var Friend = new new the Person (); Alert (Friend the instanceof Object); // to true Alert (Friend the instanceof the Person); // to true Alert (== friend.constructor the Person); // to false Alert (friend.constructor == Object); // to true var keys2 = Object.keys (Person.prototype); Alert (keys2); // constructor, name, age, job, sayName // simple prototype syntax manually set after the constructor property, it will lead to [Enumerable] characteristics true, the default constructor is native to false // reset constructor, only ECMAScript5 suitable compatible browser Object.defineProperty (Person.prototype, "constructor" , { Enumerable: to false , value: the Person }); var keys3 = Object.keys (Person.prototype); Alert (keys3); // name, Age, Job, sayName // / * ******************************** ************************************************** ********** * / / * *********************************** 4 the prototype of the dynamic *************************************** * / var per = new new the Person (); // can always add properties and methods to the prototype, and all such modifications can be immediately reflected in all object instances. = Person.prototype.sayHi function () { Alert ( "the Hi!" ); } Per.sayHi (); // if reconstruct the entire prototype object, when calling the constructor will add a pointer to the first instance of the prototype [ prototype] pointer, and modify the prototype is equivalent to another object cuts off communication between the constructor and the initial prototype. //The pointer to the prototype example only and is not a constructor. function Student () {}; var= ST new new Student (); // create an object instance Student.prototype = { // reconstructed prototype prototype object constructor: Student, name: "the Nike" , Age: 20 is , speakEnglish: function () { Alert ( "the Hello! " ); } }; st.speakEnglish (); // error 0: Object or does Not Support Property Method 'speakEnglish' // / ******************* ************************************************** ************************* / / * ** ********************************* the prototype problem ************ *************************** * / // prototypes of all attributes are shared by many examples of such a function is very suitable for sharing. There is no worth to contain the basic attributes of the same name by adding a property on the prototype example, you can hide the corresponding property. // however, contains a reference value for the type, it will be a problem. In the following example. function Singer, () {}; Singer.prototype = { constructor: Singer,, name: "the Nike" , Age: 30 , Albums: [ "Suger", "Live"] // "Suger", "Live" all instances of objects the shared set of albums }; var s1 = new new Singer, (); var S2 = new newSinger, (); s1.albums.push ( "Flowers"); // "Flowers" is s1 album Alert (s1.albums); // "Suger", "Live", "Flowers" Alert (s2.albums ); // "Suger", "Live", "Flowers", and "Flowers" is not the album s2 Alert (s1.albums === s2.albums); // point to the same albums / * ***** ************************************************** ************************************* * / </ Script> </ head> <body> </ body> </ HTML>
Partly from "JavaScript Advanced Programming (3rd Edition)"