JavaScript Object-Oriented programming: How to define property fields

Source: Internet
Author: User
Tags constructor define local instance method

We all know that the JS variable has a scope concept, so you can use this attribute to define the private field, and the initialization of the private field is mainly through the constructor.

For example, the following defines a read-only field first

function ListCommon2 (Afirst)  
 {  
   var first=afirst;  
   This. Getfirst=function () defines a privileged method to access the Read field {return a       
      ;}     
 Listcommon2.prototype.do2=function ()  
 {         
     var field= this. GetFirst ();//Read field alert in the instance method using the privileged method  
     ;  
 }    
             
     function test () {  
                //test code  
             var t2=new ListCommon2 ("Boiling water 2");            
             T2.do2 ();//                  
     }

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

Of course, if you need a method to modify a field, you can also define a privileged method that is similar to reading a field.

As follows:

function ListCommon2 (Afirst)  
 {  
   var first=afirst;  
   This. Getfirst=function () defines a privileged method to access the Read field {return a.  
    }   
    This. Setfirst=function (NewValue)//defines a privileged method to set the field  
    {return       
      first=newvalue  
    }     
 } Listcommon2.prototype.do2=function ()  
 {         
     var field= this. GetFirst ();//Read field alert in the instance method using the privileged method  
     ;  
 }    
             
     function test () {  
                //test code  
             var t2=new ListCommon2 ("Boiling water 2");            
             T2. Setfirst ("test");//Reset field  
             t2.do2 ();//                  
     }

So the definition field can define local variables in the constructor, using the privileged method as a way to read and set fields, which can be accessed indirectly through privileged functions.

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.