Three JavaScript class members in AJAX

Source: Internet
Author: User
All class members defined before the public members of the implementation class belong to the category of public members. any instance of the class openly discloses these attributes and methods. Private members of the implementation class are members that can be shared in the internal implementation of the class. Public member of the implementation class

Any class member defined above belongs to the category of public members. any instance of this class openly discloses these attributes and methods.

Private member of the implementation class

Private members are members that can be shared in the internal implementation of the class. they are not openly disclosed. JavaScript does not have a special mechanism to define private members, but some techniques can be used to achieve this effect.

This technique is mainly implemented by the scope of a variable. in JavaScript, a variable defined in a function is called a local variable and cannot be accessed by programs outside the function, but can be visited by nested functions defined inside the function. In the process of implementing private members, it is the use of this nature.

As mentioned above, you can think of adding a member to a class in the class structure function. the class members defined by this method actually share the local variables defined inside the structure function, these variables can be seen as private members of the class, for example:

<Script language = 'javascript 'type = 'text/JavaScript '>
<! --
Function class1 (){
Var pp = 'This is a private property '; // private property member pp
Function pm () {// private method member pm, displaying the pp value
Alert (pp );
}
This. method1 = function (){
// Change the value of a private attribute in a public member
Pp = 'pp has been changed ';
}
This. method2 = function (){
Pm (); // call a private method in a public member
}
}
Var obj1 = new class1 ();
Obj1.method1 (); // call the public method method1
Obj1.method2 (); // call the public method method2
// -->
</Script>

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.