// When there are many attributes and methods, writing is not very convenient. You can write them in json format.
// Because the prototype is not specified through Person. prototype, the constructor will not point to Person but to Object.
// If constructor is really important, you can describe the prototype in json.
Function Person (){
}
Person. prototype = {
// Constructor: Person, // manually specify a constructor
Name: "octopus ",
Age: 23,
Say: function (){
Alert (this. name + ":" + this. age );
}
}
Var p1 = new Person ();
P1.say (); // octopus: 23
Alert (p1.constructor = Person); // false
Person. prototype. sayHi (){
Alert (this. name + ": hi ");
}
P1.sayHi (); // octopus: hi
// If the prototype is rewritten to new Person (), p1.sayHi (); // undefined: hi
Function Person (){
}
Var p1 = new Person ();
Person. prototype. sayHi (){
Alert (this. name + ": hi ");
}
P1.sayHi (); // undefined: hi
Person. prototype = {
// Constructor: Person, // manually specify a constructor
Name: "octopus ",
Age: 23,
Say: function (){
Alert (this. name + ":" + this. age );
}
}
P1.sayHi (); // undefined: hi has sayHi (), but no name
Var p2 = new Person ();
P2.sayHi (); // error: sayHi is not a function. This is because p2 does not have sayHi
Original article reprinted, please indicate the source, this article first in the csdn Website: http://blog.csdn.net/magneto7/article/details/24922481