JS Object-Oriented programming: JS class definition function when prototype and this difference?

Source: Internet
Author: User
Tags instance method

In the object-oriented Programming JS script, there are two main ways to define an instance

For example, the following:

function ListCommon2 (afirst) {   var first=afirst;   This.do1=function () {    alert ("First do" +first);}                  }   Listcommon2.prototype.do2=function () {     //  alert ("First do" +first);//error. No access to Firstthis.do1 (); }  

What is the difference between this.do1=function () and listcommon2.prototype.do2=function ()?

is equivalent to an instance method of a class, which is used only with new talent. What difference does it have?

Test code:

var t2=new ListCommon2 ("Boiling water 2");     T2.do1 ();//T2.do2 ();//

Test finds: This.do1 is able to access the variable first inside the constructor. ListCommon2.prototype.do2 cannot access, but can access the function This.do1.

If you define LISTCOMMON2.PROTOTYPE.DO2 within the constructor, you will be able to access the questions. But as an instance function. The assumptions are defined inside the constructor, and each instantiation is run. Obviously, it's not reasonable to waste memory.


Some of the data on This.do1 are called privileged methods. The main purpose is to access the internal private fields so that you can control access to certain fields. For example, a private field first is defined, and can only be passed through a constructor, and then it cannot be changed.

ListCommon2.prototype.do2 such methods are equivalent to the instance methods of the class. However, you can access these privileged methods and indirectly access private fields.


Conclusion:

Suppose you want to access private fields directly, you should use the privileged method, which is the method defined by this. Should be defined inside the constructor. Instead, suppose you do not need to access private fields directly, you should use the method defined by prototype. and should be defined outside of the constructor function.




JS Object-Oriented programming: JS class definition function when prototype and this difference?

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.