JavaScript Object-Oriented programming: The difference between prototype and this when the JS class defines a function

Source: Internet
Author: User
Tags constructor instance method

In object-oriented programming JS script, there are two main ways to define instance methods

As follows:

function ListCommon2 (Afirst)  
 {  
   var first=afirst;  
   This.do1=function ()  
    {       
      alert (' +first ');  
    }     
  Listcommon2.prototype.do2=function ()  
 {       
      //  alert ("+first")//error, no access to the A-  
        this.do1 () ;  
 }

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

is equivalent to a class instance method, only new to use, what is the difference?

Test code:

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

The test found that THIS.DO1 can access the variables within the constructor, and ListCommon2.prototype.do2 cannot access it, but can access the function This.do1.

If you define the ListCommon2.prototype.do2 inside the constructor, you can also access it. However, as an instance function, if defined within the constructor, each instantiation is performed, apparently wasting memory and unreasonable.

Some data this.do1 such methods are called privileged methods, primarily to access the internal private fields, so that you can control access to certain fields. For example, on the definition of a private field of a, which can only be passed through a constructor, and then cannot be modified.

ListCommon2.prototype.do2 such methods are equivalent to instance methods of classes, but can access these privileged methods and indirectly access private fields.

Conclusion:

If you want to access private fields directly, you should use the privileged method, which is the method defined by this, which should be defined within the constructor. Conversely, if you do not need to access private fields directly, you should use the method defined by prototype and should be defined outside the constructor.

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.