JavaScript modularity and namespaces

Source: Internet
Author: User

Generally speaking, the module is a separate JS file. A module file can contain a class definition, a set of related classes, a utility library, or some code to execute. The goal of modularity is to support large-scale program development, to handle the assembly of code in a decentralized source, and to allow code to run correctly, even if it contains unnecessary module code, and can execute code correctly.

by defining the module within a function, the defined variables and functions belong to the function's local variables and are not visible outside the function. In fact, this function scope can be used as a module namespace (module function)

Once the module code is encapsulated in a function, some methods are required to export the public API to invoke them outside of the module functions. There are several ways to export a common API:

first create a namespace

Create a global variable to hold school-related modules
var School; Create a school namespace
if (!school) school = {};

1. Using the constructor function

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJoHzS0tQAAAxgZ_M63g437.png "height=" 200 "Width=" 537 "/>

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJp_RQIevAAAoAy4vl00297.png "/>




2. Call through the keyword new
Another similar technique is to call a module function as a constructor, via new. Assign them (public API) to the This property to export them

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJrmAqeQvAAAf7GmG1s4107.png "/>

3. namespace objects are defined

as an alternative, if a global namespace object has been defined, the properties of that object can be set directly through the module function.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJtnjBGgvAAAqc_XhaL4046.png "/>

The way to export the public API has been finished.


This article is from the "Add Language" blog, please make sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1547559

JavaScript modularity and namespaces

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.