This article brings you a detailed analysis and comparison of front-end modularization in JS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
require
and import
are often used when importing modules during development;
When exporting a module, use module.exports/exports
or export/export default
;
Sometimes To reference a module, require
is used. Strangely, import
can also be used? ? ? ? What's the difference between them?
So there is a search process for rookies to solve their doubts. . . . .
Allow modules to pass the require methodSynchronically load other modules that depend on, and then export the interfaces that need to be exposed through exports or module.exports.
Usage:
// 导入 require("module"); require("../app.js"); // 导出 exports.getStoreInfo = function() {}; module.exports = someValue;
Because the modules are placed on the server side, for the server side When loading the module
As for the browser side, because the modules are placed on the server side, the loading time also depends on factors such as the speed of the network. If it takes a long time, the entire The application will be blocked.
Therefore, browser-side modules cannot use "synchronous loading" (CommonJs), but can only use "asynchronous loading" (AMD).
Advantages Disadvantages:
Cannot load multiple modules in parallel without blocking
Server-side modules are easy to reuse
AMD (Asynchronous loading module)
Usage example:
// 定义 define("module", ["dep1", "dep2"], function(d1, d2) {...}); // 加载模块 require(["module", "../app"], function(module, app) {...});
Advantages:
Multiple modules can be loaded in parallel
does not conform to the general modular way of thinking, and is a compromised implementation
RequireJS's attitude towards modules is pre-execution. Since RequireJS is an executed AMD specification, all dependent modules are executed first; that is, RequireJS executes the dependent modules in advance, which is equivalent to advancing the require
RequireJS execution process:
According to the js file Actual path, insert the script node in the dom, and bind the onload event to get notification that the module is loaded.
After all dependent scripts are loaded, call the callback function
CMD specification (asynchronous loading module)
The definition module uses the global function define, which receives the factory parameter. Factory can be a function, an object or a string;
factory is a function with three parameters, function(require, exports, module):
exports is an object, used to provide the module interface to the outside world
module is an object, The above stores some properties and methods associated with the current module
define(function(require, exports, module) { var a = require('./a'); a.doSomething(); // 依赖就近书写,什么时候用到什么时候引入 var b = require('./b'); b.doSomething(); });
优点:
依赖就近,延迟执行
可以很容易在 Node.js 中运行
缺点:
依赖 SPM 打包,模块的加载逻辑偏重
实现代表库sea.js
:SeaJS对模块的态度是懒执行, SeaJS只会在真正需要使用(依赖)模块时才执行该模块
对于依赖的模块,AMD 是提前执行,CMD 是延迟执行。不过 RequireJS 从2.0开始,也改成了可以延迟执行(根据写法不同,处理方式不同)。CMD 推崇 as lazy as possible.
AMD推崇依赖前置;CMD推崇依赖就近,只有在用到某个模块的时候再去require。
// AMD define(['./a', './b'], function(a, b) { // 依赖必须一开始就写好 a.doSomething() // 此处略去 100 行 b.doSomething() ... }); // CMD define(function(require, exports, module) { var a = require('./a') a.doSomething() // 此处略去 100 行 var b = require('./b') // 依赖可以就近书写 b.doSomething() // ... });
UMD是AMD和CommonJS的糅合
AMD 以浏览器第一原则发展异步加载模块。
CommonJS 模块以服务器第一原则发展,选择同步加载,它的模块无需包装。
UMD先判断是否支持Node.js的模块(exports)是否存在,存在则使用Node.js模块模式;在判断是否支持AMD(define是否存在),存在则使用AMD方式加载模块。
(function (window, factory) { if (typeof exports === 'object') { module.exports = factory(); } else if (typeof define === 'function' && define.amd) { define(factory); } else { window.eventUtil = factory(); } })(this, function () { //module ... });
ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。
ES6 模块设计思想:尽量的静态化、使得编译时就能确定模块的依赖关系,以及输入和输出的变量(CommonJS和AMD模块,都只能在运行时确定这些东西)。
使用方式:
// 导入 import "/app"; import React from “react”; import { Component } from “react”; // 导出 export function multiply() {...}; export var year = 2018; export default ... ...
优点:
容易进行静态分析
面向未来的 EcmaScript 标准
缺点:
原生浏览器端还没有实现该标准
全新的命令字,新版的 Node.js才支持。
require使用与CommonJs规范,import使用于Es6模块规范;所以两者的区别实质是两种规范的区别;
CommonJS:
对于基本数据类型,属于复制。即会被模块缓存;同时,在另一个模块可以对该模块输出的变量重新赋值。
对于复杂数据类型,属于浅拷贝。由于两个模块引用的对象指向同一个内存空间,因此对该模块的值做修改时会影响另一个模块。
当使用require命令加载某个模块时,就会运行整个模块的代码。
当使用require命令加载同一个模块时,不会再执行该模块,而是取到缓存之中的值。也就是说,CommonJS模块无论加载多少次,都只会在第一次加载时运行一次,以后再加载,就返回第一次运行的结果,除非手动清除系统缓存。
循环加载时,属于加载时执行。即脚本代码在require的时候,就会全部执行。一旦出现某个模块被"循环加载",就只输出已经执行的部分,还未执行的部分不会输出。
ES6模块
ES6模块中的值属于【动态只读引用】。
对于只读来说,即不允许修改引入变量的值,import的变量是只读的,不论是基本数据类型还是复杂数据类型。当模块遇到import命令时,就会生成一个只读引用。等到脚本真正执行时,再根据这个只读引用,到被加载的那个模块里面去取值。
对于动态来说,原始值发生变化,import加载的值也会发生变化。不论是基本数据类型还是复杂数据类型。
循环加载时,ES6模块是动态引用。只要两个模块之间存在某个引用,代码就能够执行。
Finally: require/exports is necessary, universal and necessary; because in fact, the import/exports you write so far are eventually compiled into require/exports for execution.
Related recommendations:
js code to implement data transfer between pages
JS Tutorial--Dynamic Planning Algorithm backpack capacity problem
Explanation of knowledge about scope and closure in javaScript
The above is the detailed content of Detailed analysis of front-end modularization in JS and comparison of its differences. For more information, please follow other related articles on the PHP Chinese website!