Use somefunction. prototype for analysis with caution

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Code from jb51.net
Function Person (name ){
This. Name = name;
}
Person. prototype. SayHello = function (){
Alert ('hello, '+ this. Name );
}
Person. prototype. SayBye = function (){
Alert ('Goodbye, '+ this. Name );
}

However, sometimes, for the convenience of writing and maintenance, We will write the declaration of the public method into an object and assign it to Person. prototype, for example:
Copy codeThe Code is as follows:
// Code from jb51.net
Function Person (name ){
This. Name = name;
}
Person. prototype = {
SayHello: function (){
Alert ('hello, '+ this. Name );
},
SayBye: function (){
Alert ('Goodbye, '+ this. Name );
}
}

In this way, when this class has a large number of public methods, you do not need to maintain many Person identifiers. If the name of this class needs to be changed one day, there are only two places to change, one is the function declaration and the other is the identifier before prototype. If the previous method is used, how many public methods need to maintain N + 1 identifiers, although search replacement can be used, in terms of stability, search replacement may cause some errors, which increases the maintenance cost.

Although this method adds convenience to our maintenance, it also raises another hidden problem, that is, the loss of the constructor attribute of the class.
Copy codeThe Code is as follows:
// Code from jb51.net
Function Person1 (name ){
This. Name = name;
}
Person1.prototype. SayHello = function (){
Alert ('hello, '+ this. Name );
}
Person1.prototype. SayBye = function (){
Alert ('Goodbye, '+ this. Name );
}
// Code from jb51.net
Function Person2 (name ){
This. Name = name;
}
Person2.prototype = {
SayHello: function (){
Alert ('hello, '+ this. Name );
},
SayBye: function (){
Alert ('Goodbye, '+ this. Name );
}
}
Alert (new Person1 ('bill '). constructor );
Alert (new Person2 ('Steve '). constructor );

Running the test code above, we can find that the constructor attribute of Person1 is a constructor of the Person1 class, but the constructor attribute of Person2 is an Object, when you need to use the constructor attribute to determine the object type, a problem occurs.
Therefore, when writing JavaScript classes, if you do not need to use the constructor attribute to obtain the object type, you prefer to use the second method, however, if you need to use the constructor attribute to implement your own reflection mechanism or GetType function, use the first method.
Of course, if you do not rely on the constructor attribute when implementing your own reflection mechanism or the GetType function, both methods are acceptable. For example, you can maintain an additional member variable to identify your own type. You can also use some ready-made JS frameworks, some of which have implemented class implementation in JS, such as js. class. This depends on your individual needs.

Related Article

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.