An application code encapsulation for Javascript anonymous Functions

Source: Internet
Author: User

You can see this method in some Javascript libraries:
Copy codeThe Code is as follows:
(Function (){
// Code of all databases
})();

To be honest, I am a beginner of js. This is too scary. In these JS libraries, this function basically includes all the code in the entire library. This writing method completely surpasses my common sense. Shouldn't we make a good distinction? Shouldn't it reflect the hierarchy and functional division? How can we deal with a function. I did not dare to think about it at first. It wasn't until I started chatting with a colleague one day after using JS that I knew it was called an anonymous function. I am not familiar with anonymous functions. C #, Python, and Lua all have these functions. I checked it online and introduced many articles in detail, but my questions were not solved: Why should I write this.
I found a famous JS open-source library, JQuery. His code is a typical writing method. After a rough look at his code, I found that this code is too big and complicated, it is not suitable for me. So I went to another open-source js library swfobject, which provided a simple interface to embed the Flash control into the webpage. This code is much better. It is very short and will soon be finished. After reading it, I suddenly realized that. The truth is so simple that writing like this has only one purpose: encapsulation.
When I was a beginner at JS, I asked my colleagues how js defined private functions and variables. The answer was: js is not object-oriented and does not provide these features. I accept this answer. After all, strict encapsulation is not necessary. Now I understand that there is always a solution that can be encapsulated in languages that do not support encapsulation. The implementation method is the anonymous function. Let's look at a piece of code:
Copy codeThe Code is as follows:
// Define
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 didn't run it. Anyone who knows about js knows that this is the JS class definition method. Function F is equivalent to a constructor, and other definitions in the function are private external access to the function, such as the double function. In this way, private methods are implemented in disguise. Other members prefixed with "this." are equivalent to public members and can be accessed externally.
The reason why these libraries use a large function to wrap the entire library code is to force users to only access open APIs without exposing internal methods and variables to users. From this point, we can develop the painstaking efforts of these developers.
Here I can't help but ask again how to implement inheritance in js. I hope this answer will not be the same as the last one: not supported.

Related Article

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.