JS: In-depth prototype (under: Prototype rewrite)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.