1. Create an object
View Code
1 var person = new Object ();
2 person. name = "RuiLiang ";
3 person. age = 30;
4 person. job = "Teacher ";
5 person. sayName = function (){
6 alert (this. name );
7 };
8
9 person. sayName ();
2. Factory Model
Disadvantage: the object cannot be recognized.
View Code
1 function createPerson (name, age, job ){
2 var o = new Object ();
3 o. name = name;
4 o. age = age;
5 o. job = job;
6 o. sayName = function (){
7 alert (this. name );
8 };
9 return o;
10}
11
12 var person1 = createPerson ("A Liang", 30, "instructor ");
13 var person2 = createPerson ("Junjun", 24, "unemployed ");
14
15 person1.sayName (); // "A Liang"
16 person2.sayName (); // "Jun"
3. constructor Mode
Disadvantage: lack of encapsulation
View Code
1 function Person (name, age, job ){
2 this. name = name;
3 this. age = age;
4 this. job = job;
5 this. sayName = sayName;
6}
7 function sayName (){
8 alert (this. name );
9}
10
11 var person1 = new Person ("A Liang", 30, "instructor ");
12 var person2 = new Person ("Junjun", 24, "unemployed ");
13 person1.sayName ();
14 person2.sayName ();
4. prototype mode
Disadvantage: All attributes are shared by instances.
View Code
1 function Person (){
2}
3
4 Person. prototype. name = "ALiang ";
5 Person. prototype. age = 30;
6 Person. prototype. job = "Teacher ";
7 Person. sayName = function (){
8 alert (this. name );
9}
The hasOwnProperty () method checks whether an attribute is an instance attribute. If yes, true is returned.
Person1.hasOwnProperty ("name"); // name is not a property of person1
In OPERATOR: whether an attribute accessed through an object exists. If yes, true is returned, regardless of whether the attribute exists in the instance or in the prototype.
Alert ("name" in person1); // if the name attribute exists, true is returned.
Determine whether the property is in the prototype or in the object:
1 function hasPrototypeProperty (object, name ){
2 return! Object. hasOwnProperty (name) & (name in object );
3}
4 // usage
5 var person = new Person ();
6 alert (hasPrototypeProperty (person, "name"); // true
7 person. name = "Grey"; // change the name value in the prototype.
8 alert (hasPrototypeProperty (person, "name"); // false
The isPrototypeOf () method is used to determine whether the specified object object1 exists in the prototype chain of another object object2. If yes, true is returned. Otherwise, false is returned.
The format is as follows:
Object1.isPrototypeOf (object2 );
Object1 is an object instance;
Object2 is another object that will check its prototype chain.
The prototype chain can be used to share functions between different instances of the same object type.
If the prototype chain of object2 contains object1, The isPrototypeOf method returns true.
If object2 is not an object or object1 does not appear in the prototype chain of object2, The isPrototypeOf method returns false.
1 // literally rewrite the prototype object
2 function Person (){
3}
4
5 Person. prototype = {
6 constructor: Person,
7 name: "ALiang ",
8 age: 30,
9 job: "Teacher ",
10 sayName: function (){
11 alert (this. name );
12}
13 };
5. constructor and prototype hybrid mode
It has the advantages of the constructor mode and the prototype mode. The constructor mode is used for attributes, and the method uses the prototype mode. // This mode is the most widely used.
View Code
1 function Person (name, age, job ){
2 this. name = name;
3 this. age = age;
4 this. job = job;
5 this. friends = ["xuyun", "wuxueming"];
6}
7 Person. prototype = {
8 constructor: Person,
9 sayName: function (){
10 alert (this. name );
11}
12 };
13
14 var person1 = new Person ("ALiang", 30, "Teacher ");
15 var person2 = new Person ("ZuNan", 26, "Teacher ");
16 person1.friends. push ("JunJun ");
17 alert (person1.friends); // "xuyun", "wuxueming", "JunJun"
18 alert (person2.friends); // "xuyun", "wuxueming"
6. Dynamic Prototype
View Code
1 function Person (name, age, job ){
2 this. name = name;
3 this. age = age;
4 this. job = job;
5
6 if (typeof this. sayName! = "Function") {// The sayName can be any method or attribute that exists after initialization.
7 Person. prototype. sayName = function () {// cannot be in the literal form
8 alert (this. name );
9 };
10}
7. Parasitic constructor Mode
8. Safe Construction of the function mode
From Sunday walk