Home  >  Article  >  Web Front-end  >  Modular analysis of js (namespace)

Modular analysis of js (namespace)

不言
不言Original
2018-07-23 11:23:361771browse

The content shared with you in this article is about the modular analysis (namespace) of js. It has certain reference value. Friends in need can refer to it.

The code is organized into modules in order to allow the code to be modularized, that is, to reuse modules in different scenarios.
A module is an independent js file. A module file can contain a class definition, a set of related classes, a utility function library, or some code to be executed. Write code in the form of modules, and the js code segment can be regarded as a module.
The goal of the module is to support program development, handle code assembly from dispersed sources, and enable the code to execute correctly

Objects used as namespaces

To avoid polluting global variables, use a The object serves as a namespace, storing the values ​​of functions and the attributes of the namespace object (that is, referenced through global variables)

var collections;  // 先声明一个全局变量
if (!collections) // 如果它原先不存在
  collections = {}; //创建一个新对顶层命名空间,加判断是因为防止出现覆盖
collections.sets = {};  // 将sets命名空间创建在其内部

// 下面开始定义相关的类
collections.sets.AbstractSet = function(){};

If you often use the set class in the sets namespace, set the set class Import into the global namespace

var Set = sets.Set; // 将Set倒入到全局命名空间中
var s = new Set();  // 这样就不用加set前缀了

A convention, the module file and the namespace should match,
For example, the file of the module using com.davidflanagan.collections.sets should be in com/davidflanagan /collections/sets.js
The above is the path, a function that agrees on the

private namespace

Exports some APIs outside the module for use by others, including functions, Properties, classes, methods.
The implementation of the module requires some auxiliary functions and methods
Functions and methods are not visible as private namespaces.
Use function scope as a private namespace, that is, module function

/*
 *    模块函数中的Set类
 *  时间:2018/07/22 13:15
 */

 // 声明全局变量Set, 使用一个函数返回值给其赋值
 // 函数结束后紧跟着圆括号,立即执行
 // 将其返回值赋值给Set
 // 这为函数表达式,没有创建函数变量
var Set = (function(){
    function Set() {    // 这个构造函数为局部变量
        this.values = {};    // 这个对象用来保存这个集合
        this.n = 0;    // 集合中的个数
        this.add.apply(this, arguments);    // 将所有的参数添加到集合中
    };

    // 给Set.prototype定义实例方法
    Set.prototype.contains = function(vale) {
        // 这里调用v2s
        return this.values.hasOwnProperty(v2s(value));
    };
    Set.prototype.size = function() {/*...*/}

    // 这里的为辅助函数和变量
    // 这里的变量不属于公有的api,但是都隐藏在函数的作用域内
    // 因此,不需要将其定义为Set属性,或使用下划线用来标识
    function v2s(val) {/*...*/};

    // 这个模块的共有api为Set()构造函数
    // 我们需要把这个函数从私有命名空间导出,以便在外部可使用,通过返回构造函数的方式导出
    // 它会变成构造函数所指的值
    return Set;
}());    // 定义函数后立即执行

Modular analysis of js (namespace)

Related recommendations:

Object.defineProperty( in JavaScript ) method analysis

Expansion of classes in js and analysis of object-oriented technology

The above is the detailed content of Modular analysis of js (namespace). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn