Implementation of Javascript class and Static Class (cont.) _js Object-oriented

Source: Internet
Author: User
Tags closure static class
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.

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.