This time we look directly at the example:
Copy Code code as follows:
/*** defines a static class ***/
var Staticclass = (function () {
var return = {
Property: ' Test Static ',//public properties
Method:function () {//Public method
alert (_field); Calling private fields
Privatemethod (); Invoke Private method
}
}; Define the returned public object
var _field = "Test Static Field"; Private field
var privatemethod = function () {//Private method
alert (Return.property); Call Properties
}
return return; Generating public static elements
})();
This time, I use the closure of the way to achieve, one of the most important point is reutrn return; This will extend to the concept of value type and reference type. In JS, object is a reference type, and in the closure I throw a reference to an object containing attributes and methods, which, in turn, throws the object outside and makes it public. Staticclass This variable catches the return reference. Private fields and methods within the closure are not thrown, so that a private environment can be formed.
Most of the time we will use this technique to block JS, so that the program will not be so confusing.
The above examples of the brothers who have not done so can also try their own. Forget, just that example you can try it like this.
Copy Code code as follows:
Staticclass.method ();
Staticclass.property = "Test2";
Staticclass.method ();
How far a man can go depends on who he walks with.