Introduction to JS modules and namespaces _ javascript skills

Source: Internet
Author: User
Introduction to JS modules and namespaces. For more information, see Cause
One important reason for organizing code into a class is to make the code more "modular" and implement code reuse in many different scenarios. However, classes are not the only way to modularize code.

Generally, a module is an independent JS file. A module file can contain a class definition, a group of related classes, a function library, or code to be executed.

The purpose of modularization is to support large-scale program development, Process Code assembly in scattered sources, and make the code run correctly. Even if the module code is not required, the code can be correctly executed.

Ideally, no more than one global ID should be defined for all modules.

Module functions
The module is defined within a function. The Defined variables and functions belong to the local variables of the function and are invisible outside the function. In fact, we can use this function scope as a module namespace (module function)

Once the module code is encapsulated into a function, some methods are required to export public APIs so that they can be called outside the module function. There are several ways to export public APIs:

First create a namespace

The Code is as follows:


// Create a global variable to store the school-related modules.
Var school; // create a school namespace
If (! School) school = {};

1. Use Constructor

The Code is as follows:


// Return the Student constructor to export the public API
School. Student = (function (){
Function Student (){

}
// ...... Define the prototype object and private attributes and methods of Student ........
Return Student; // return the Student constructor to export the public API
})();

2. Return the namespace object

If the module API contains multiple units, it can return the namespace object.

The Code is as follows:


// Add the students module to the school
School. students = (function (){
// Many classes are defined here, such as course classes/score classes, using local variables and functions
Function Subject (){/*...*/}
Function Grade (){/*...*/}

// Export the API by returning the namespace object
Return {
Subject: Subject,
Grade: Grade
};
})();

3. call with the keyword "new"

Another similar technology: the module function is called by using new as the constructor. Assign them (Public APIs) to the this attribute to export them.

The Code is as follows:


School. students = (new function (){
// ...... The code is omitted here ......

// Import the API to this object
This. Subject = Subject;
This. Grade = Grade;

// Note that there is no returned value
} (); // Brackets are written in it. Here is the creation of a new instance. The new should be followed by the call of the constructor rather than the expression.

4. The namespace object has been defined.

As an alternative, if a global namespace object has been defined, you can directly set the attributes of that object through the module function.

The Code is as follows:


// If the namespace object has been defined
Var school; // create a school namespace
If (! School) school = {};
School. students ={}; // The student namespace has been defined.
(Function (students ){
// ...... The code is omitted here ......

// Export the public API to the namespace defined above
Students. Subject = Subject;
Students. Grade = Grade;
// No return value is required here.
}) (School. students );

To this end, the export of Public APIs has been completed.

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.