JavaScript object-oriented private member and Public Member _ js object-oriented

Source: Internet
Author: User
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! The last two sections talk about the JavaScript object-oriented namespace and the javascript object-oriented JavaScript class. You can read the above before proceeding.


In fact, it is very simple. I believe you will be clear at a glance after reading the following code and comments!

The Code is as follows:


// Declaration class is a method. In JavaScript, namespace, class, and member... everything is an object.
MyClass = function (){
Var _ this = this;
// Private variable
Var aa = "11 ";
// Publish Variables
This. bb = "22 ";
// Private Method
Function fun1 (){
Alert (aa );
Alert (_ this. bb );
}
// Private Method
Var fun2 = 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
Variables or methods declared with the var keyword are private;
The method declared with the function keyword is private;
Variables or methods declared using the this 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!
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.