Thinking about the application of JavaScript closure

Source: Internet
Author: User

First, the importance of modularization in software development

In the object-oriented world, the class is undoubtedly the most important concept, she effectively to the software system in the various parts of the modular separation, to avoid the impact of each other. In the process-oriented language development process, often found that the code is always redundant, and think about to ctrl-c ctrl-v, the problem of naming conflicts, so that the whole software system is not easy to understand, and greatly increase the difficulty of development, difficult to develop a high degree of complexity of the software. This is the idea of object-oriented ideas and significance, I believe you are very familiar with the object-oriented thinking, its core is nothing more than a class to describe the entire software system business logic, in order to achieve the system modularity, code reuse purposes. Class has a very important feature, that is, what is defined in the class is public, what is private, this is very meaningful to achieve modularity, if not these, everything is a global variable, then how to module partition this say?

Second, implement public private in JavaScript

JS is different from the mainstream object-oriented language, in my opinion, JS is more like a functional programming language with process-oriented syntax, is a multi-paradigm language. In other words, you can write a JS program like C, or it can be written like an object-oriented language, and can be written as a functional language, depending on what solution you take to the problem space.

  

In JS, if you want to achieve modularity, the first problem is to solve the public and private problems of the module, in JS, using the characteristics of closures to accomplish this task. The concept of closures can be described simply as follows, assuming that the functions Funca and FUNCB,FUNCB are defined in Funca, then the variables defined in Funca can be used in FUNCB, so the external can manipulate the variables in FUNCB by Funca. And at the end of the life cycle of the Funca, FUNCB still maintains a reference to the Funca variable and is not released, a feature called closure. Perhaps someone will ask: why use Funca directly to manipulate the variables in Funca? Yes, it does, but when Funca is an anonymous function, the outside cannot invoke the variable in the Funca by an explicit method. Bingo! is to use this feature to achieve the private/public in JS, see the following code:

1 varJsclass = (function(){2         varThisClass, privatevar= "I ' m private";3Privatefunc =function() {alert ("I ' m private")};4 5Thisclass.publicfunc =function() {alert ("I ' m public")};6Thisclass.publicvar = "I ' m public";7 8         returnThisClass9}())

The code is clear, through anonymous functions and the nature of closures, We can use Jsclass.publicfunc and jsclass.publicvar to access externally exposed functions and variables, while privatevar,privatefunc is externally invisible, that is, private members.

In this regard, the public private problem in JS has been solved perfectly, in the world of JS, many libraries have used this feature to achieve the purpose of modularization, the typical is Requirejs, in the JS world, everywhere shining the function of the star, waiting for you to find, find, appreciate.

Thinking about the application of JavaScript closure

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.