Home >Web Front-end >JS Tutorial >Implementation of Javascript classes and static classes (continued)_js object-oriented

Implementation of Javascript classes and static classes (continued)_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:30:271123browse

This time we look directly at the example:

Copy the code The code is as follows:

/***Define static class***/
var StaticClass = (function(){
var Return = {
Property: "Test Static Property", //Public property
Method: function(){ //Public method
alert(_Field); //Call private field
privateMethod(); //Call private method
}
}; //Define the returned public object
var _Field = "Test Static Field"; //Private field
var privateMethod = function(){ //Private method
alert(Return.Property); //Call property
}
return Return; //Generate public Static element
})();

This time, I used closures to implement it. The most important point is reutrn Return; which will extend to value types and references. Type concept. In js, Object is a reference type. In the closure, I will throw a reference to the Object that contains properties and methods. In this way, it can also be said that the Object is thrown to the outside and made public. The variable StaticClass receives the Return reference. The private fields and methods inside the closure are not thrown, thus forming a private environment.
Many times we use this technique to chunk JS so that the program will not be so confusing.
Brothers who have never done this in the above example can also try it themselves. Forgot, you can try the example just like this.
Copy code The code is as follows:

StaticClass.Method();
StaticClass.Property = "Test2";
StaticClass.Method();

How far a person can go depends on who he travels with
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn