JS Module Development (i)

Source: Internet
Author: User

Now embedded in the page inside the JavaScript code is more and more complex, so may rely on more and more serious, using other people to develop more and more JS, so in the ideal case, we only need to implement the core business logic, others can load others have already written modules.

JS Modular development is becoming more and more important, but JavaScript is not a modular programming language, it does not support the class, not to mention the module. (The ECMAScript standard Sixth Edition, which is under development, will officially support "Class" and "module", but it still takes a long time to put into practice.) In existing development, use the effect of the module. So we simply summed up several ways of module development.

(1) Original wording:

A module is a set of methods for implementing a specific function.

Simply put the different functions (and the variables that record the state) together, even if it is a module.

Function F1 () {}function F2 () {}

Then we can call directly, but this approach has a lot of drawbacks. For example: "Pollute" the global variable, there is no guarantee that the variable name conflicts with other modules, and there is no direct relationship between the module members.

(2): Use of the object's wording

In order to solve the above shortcomings, the module can be written as an object, all the module members are placed in this object. This notation is often used in my current project.

var Func={var count=0;inti:function () {},getvalues:function () {}}

And then we're going to use the words directly func.inti (). But there is a drawback to this approach: this way of writing exposes all module members, and the internal state can be rewritten externally. Func.count=1, it's not safe.

(3): Execute function immediately

This prevents changes to the internal members.

var func= ({var count=0;inti:function () {},getvalues:function () {}}) ():

Func is the basic wording of the JavaScript module. Can play a very good role in protecting private members.

(4): Magnification mode

If a module is large and must be divided into several parts, or if one module needs to inherit another module, then it is necessary to use "magnification mode"

var func = (function (fn) {fn.m = function () {//...};  return FN; }) (func)

The above code adds a new method M () to the Func module and returns the new Func module.

(5): Wide magnification mode

In a browser environment, parts of the module are usually obtained from the web, and sometimes it is not possible to know which part is loaded first. If you use the previous section, the first part of the execution is likely to load a nonexistent empty object, then use the "wide magnification mode".

var func = (function (FN) {//... return fun; }) (Window.func | | {});

In contrast to Loupe mode, the wide magnification mode is a parameter that executes function immediately, which can be an empty object.

(6): Enter global variables

Independence is an important feature of the module and it is best not to interact directly with other parts of the program. In order to call global variables inside a module, you must explicitly enter other variables into the module.

var func = (function ($, Baidu) {//...}) (JQuery, Baidu);

The Func module above needs to use the jquery library and the Baidu Library, the two libraries (actually two modules) as parameter input func. In addition to ensuring the independence of the module, it also makes the dependency between modules obvious.

To be continued: JS Module loader

JS Module Development (i)

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.