JavaScript: three members of the JavaScript class in AJAX, public members of the implementation class in the Javascript tutorial
All the class members defined above belong to the category of public members. any instance of this class 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 and are not disclosed to the public. JavaScript does not have a special mechanism to define private members, but some tips can be used to implement this function.
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 accessed by nested functions defined in the function. In the process of implementing Private Members, it is the use of this nature.
As mentioned above, you can add members to a class in the class constructor. The class members defined in this way actually share the local variables defined inside the constructor, 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> |
The running result is displayed.
In this way, the private property pp and the private method pm are implemented. After class1 is run, although it seems that the pp and pm local variables should disappear immediately, but actually because class1 runs through new, the object to which it belongs has not disappeared, therefore, you can still operate on the public members.
Note: These local variables (private members) are shared by all public methods defined in the constructor and only shared by the public methods defined in the constructor. This means that the class members defined in prototype cannot access the local variables (Private Members) defined in the constructor ).
Private Members are used at the cost of code readability. In addition, this implementation is more of a JavaScript technique, because it is not a mechanism of the language itself. However, this technique of using the scope of variables is worth learning.
Implement static members
A static member belongs to a class member. It can be accessed through the "class name. static member name" method. In JavaScript, you can directly add members to a function object to implement static members. Because a function is also an object, object-related operations are also applicable to functions. For example:
Function class1 () {// Constructor } // Static attributes Class1.staticProperty = "sample "; // Static method Class1.staticMethod = function (){ Alert (class1.staticProperty ); } // Call the static method Class1.staticMethod (); |
The above Code adds a static attribute and a static method for the class class1, and references the static attribute of the class in the static method.
If you want to add a common static method to each Function object, you can also use the class Function corresponding to the Function object. For example:
// Add the Prototype Method to the class Function: show ArgsCount Function. prototype. showArgsCount = function (){ Alert (this. length); // display the number of parameters defined by the function } Function class1 (){ // Define a class } // Call the showArgsCount static method of the class defined by Function prototype Class1. showArgsCount (); |
It can be seen that through Function prototype object, can add common static members to any Function, which can play a great role in actual development, such as in the famous prototype-1.3.1.js framework, the following two methods are defined for all functions:
// Run the function as an object Method Function. prototype. bind = function (object ){ Var _ method = this; Return function (){ _ Method. apply (object, arguments ); } } // Use the function as the event listener Function. prototype. bindAsEventListener = function (object ){ Var _ method = this; Return function (event ){ _ Method. call (object, event | window. event ); } } |
These two methods play a major role in the prototype-1.3.1 framework. The specific meanings and usage will be described in the following sections.