When the properties and methods are particularly long, they are not very convenient to write, and can be written in JSON format.
Because of the prototype rewrite, and not specified by Person.prototype, at this point the constructor no longer points to the person but points to object
If constructor is really important, you can illustrate the point of the prototype in JSON
function person () {
}
Person.prototype = {
Constructor:person,//manually specify 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
But if the prototype rewrite is placed after the 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 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 has no sayhi
original articles such as reproduced, please indicate the source, this article started in CSDN website: http://blog.csdn.net/magneto7/article/details/24922481