Comparison of class method, construction method and prototype method in JavaScript

Source: Internet
Author: User
Tags comparison instance method

If you have been in touch with JS for a long time, then you should be able to see whether I summed up the correct, if you have just started to learn, then through my summary, you can distinguish them faster, remember I just contact JS, this piece is fuzzy for a long time!

1, the difference of looks:

1 function Class () {//Declaration of a class

2 This.constructmethod = function () {}; Adding a constructed construction method

3};

4 Class.classcmethod = function () {}; Adding class methods

5 class.prototype.protocmethod=function () {};//Add prototype method

2, the difference of usage:

1 Class.classmethod ();//class method call directly

2 var instance = new Class ();

3 Instance.constructmethod ()///To construct the method instance to invoke the

4 Instance.protomethod ()///prototype method instance to invoke

3, the difference in performance:

A Class A method can only have one copy in memory because it belongs to the class itself, and in practice we do not use the class method generally. The main thing to write is to let you know it.

Both the B construction method and the prototype method are instances, but the construction method produces a new copy in memory each time new Class (). Usually this method is used to differ between instances. The construction methods for each instance do not affect each other. But apparently, it takes up memory again. The prototype method is just the opposite, it does not follow the new Class ()

Produces a new copy, which is only available in memory. You can implement sharing between instances. It also saves memory.

In summary: You generally do not use the class method in the development, will have the common method to make the prototype method, will have the individuality method to make the construction method.

4, the construction method mentioned above can also be transferred to the instance in the actual project, that is, to add the method to the instance, because we usually put some attribute members instead of methods in the constructor of the class. See below:

1 function Class () {};

2 var instance = new Class ();

3 instance.instancefn=function () {};//Add instance method

Author: cnblogs Wimber

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

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.