Object-oriented JavaScript (3): private and public members

Source: Internet
Author: User

For Javascript in small projects, you only need to write a few functions. However, in large projects, especially in developing websites that require good user experience, such as SNS, a large amount of javascrpt will be used. Sometimes the Javascript workload is better than C #, at this time, writing a bunch of functions will become messy, messy, and even name conflicts, making management and maintenance troublesome. In this case, we need to use the object-oriented idea to develop JavaScript. Let's do this:

This section describes the private and public members of JavaScript. Although JavaScript does not have private and public keywords, it is still the same sentence-as developers, we need to have an object-oriented idea!

In fact, it is very simple. I have read the followingCodeAnd comments are clear to you!

 

// A declarative class is a method. In JavaScript, namespaces, classes, and members... all objects are objects.
Myclass = Function (){
VaR _ This = This ;

//Private variable
VaRAA="11";

//Public Variables
This. Bb="22";

//Private Method
FunctionFun1 (){
Alert (AA );
Alert (_ this. bb );
}

//Private Method
VaRFun2=Function(){
Alert (AA );
Alert (_ this. bb );
}

//Public Method
This. Fun3=Function(){
Alert (AA );
Alert (_ this. bb );
}
}

// The test is as follows:
VaR MC = New Myclass ();
MC. AA = " AA " ; // Error

MC. bb = " Bb " ; // Correct

MC. fun1 (); // Error

MC. fun2 (); // Error

MC. fun3 (); // Correct

In a word: Inside the class

UseVaRThe variables or methods declared by the keyword are private;

UseFunctionThe keyword declaration method is private;

UseThisThe variables or methods declared by the keyword are public.

 

All of the above are for instance classes, and static classes are simpler. Javascript static classes are actually a JSON object, so all its members are public, they are all externally visible!

 
Address: http://www.cnblogs.com/xumingxiang/archive/2010/04/30/1725364.html

Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.

 

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.