JavaScript-oriented private and public member _js object-oriented

Source: Internet
Author: User
Tags static class
The last two sections talk about JavaScript object-oriented namespaces and JavaScript-oriented JavaScript classes, so you can look at the above and continue to look down.


In fact, it is very simple, less talk, read the following code and comments believe you will be at a glance!

Copy Code code as follows:

Declaring a class is a method, in fact, in JavaScript, namespaces, classes, members .... All objects
MyClass =function () {
var _this=this;
Private variable
var aa= "11";
Exposing variables
this.bb= "22";
Private method
function Fun1 () {
alert (AA);
alert (_THIS.BB);
}
Private method
var fun2=function () {
alert (AA);
alert (_THIS.BB);
}
Exposing methods
This.fun3=function () {
alert (AA);
alert (_THIS.BB);
}
}
The test is as follows:
var mc=new MyClass ();
Mc.aa= "AA";//Error
mc.bb= "BB";/Right
MC.FUN1 ();//Error
Mc.fun2 ();//Error
MC.FUN3 ()//correct


Word: In the interior of 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 with the This keyword are public.

All of the above is for instance classes, and for static classes that is simpler, JavaScript static class is actually a JSON object, so all its members are public, are 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.