An application code encapsulation _javascript technique for JavaScript anonymous functions

Source: Internet
Author: User
You can see this in some JavaScript libraries:
Copy Code code as follows:

(function () {
All library Code Code
})();

To tell the truth, for JS beginners for me. This thing is so scary, in these JS libraries, this function basically put the entire library of all the code, which is completely beyond my common sense. Should not be a good division, it should not reflect the hierarchy and functional division, how can a function to get it done. At first I was completely afraid to think about it. Until the use of JS for a period of time, one day a colleague in the chat talk about this problem, I know this is called anonymous function. Anonymous functions I am no stranger to this c#,python,lua. I went to the internet to check a bit, introduced a lot of articles, but also very detailed, but my heart's doubts have not been resolved: why to write this.
I found a well-known JS open Source Library jquery, his code is typical of the above, after a cursory look at his code I found that this set of code is too big too complex, it is not suitable for me. So I turned to see another open source JS library SWFObject, which provides a simple interface for embedding flash controls into a Web page. This code is much better, very short, and soon finished. After reading it, I suddenly realized that. The truth is so simple, so written, only one purpose: encapsulation.
Beginner JS, I asked colleagues, JS How to define private functions and variables. Then the answer is: JS is not object-oriented, do not provide these features. I am also comfortable with this answer, after all, strict packaging is not necessary. It is now understood that there are always ways to implement encapsulation in languages that do not support encapsulation. And the way to do that is by anonymous functions. Look at a piece of code:
Copy Code code as follows:

Defined
function F (x)
{
this.x = x;
function double (x) {return x*x;}
This.getdoublex () {
return double (this.x);
}
}

Use
F = new F (12);
Alert (F.getdoublex ());


The above code is very simple, I did not go to run it. Understand the point JS all know this is JS class definition way. function f is equivalent to a constructor, and other definitions within a function are inaccessible outside of the function's private, such as the double function. This means that the private method is implemented in disguise. The other hits "this." The members of the prefix are equivalent to public members and can be accessed externally.
The reason these libraries use a large function to wrap the entire library code is to force users to access only the Open APIs in order not to expose internal methods and variables to the consumer. From this point, we can draw the good intentions of these developers.
Here I can't help but ask again, JS how to achieve inheritance. Hope this time the answer is not the same as last time: not supported.

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.