What is the difference between es6 and commonjs
Difference: 1. CommonJS outputs a copy of the value, and ES6 outputs a reference to the value; 2. CommonJS is loaded at runtime, and ES6 is a compile-time output interface; 3. CommonJS require is loaded synchronously Modules, ES6's import is loaded asynchronously, and there is a resolution phase for independent module dependencies.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
What is the difference between es6 and commonjs
1. The CommonJS module outputs a copy of the value, while the ES6 module outputs a reference to the value
Let’s take a look at the usage of commonjs
1. First create a lib.js file
// lib.js const counter = 3; const incCounter = ()=>{ counter++ } module.exports = { counter, incCounter }
2. Create a main.js again and import it using commonjs
// main.js var lib = require('./lib'); console.log(lib) console.log(lib.counter); // 3 lib.incCounter(); console.log(lib.counter); // 3
After the lib.js module is loaded, its internal changes will not affect the output lib.counter. This is because mod.counter is a primitive type value and will be cached; let’s take a look at the usage of
esmodule
// lib.js export let counter = 3; export function incCounter () { counter++; }
// main.js import { counter, incCounter } from './util.mjs' console.log(counter); //3 incCounter() console.log(counter) //4
ES6 module does not cache the running results, but dynamically Get the value from the loaded module locally, and the variable is always bound to the module in which it is located.
Supplement: Variables imported through esmodule cannot be reassigned or modified.
2. The CommonJS module is loaded at runtime, and the ES6 module is the output interface at compile time
// CommonJS模块 let { stat, exists, readFile } = require('fs'); // 等同于 let _fs = require('fs'); let stat = _fs.stat; let exists = _fs.exists; let readfile = _fs.readfile;
The essence of the above code is to load the fs module as a whole (that is, load all fs method), generate an object (_fs), and then read 3 methods from this object. This kind of loading is called "runtime loading", because this object can only be obtained at runtime, making it impossible to do "static optimization" at compile time. Therefore, commonjs is a method of loading modules only when it is run.
import { stat, exists, readFile } from 'fs';
The essence of the above code is to load 3 methods from the fs module, and other methods are not loaded. This kind of loading is called "compile-time loading" or static loading, that is, ES6 can complete module loading at compile time, which is more efficient than the loading method of CommonJS modules;
3. CommonJS module require () is a synchronous loading module. The import command of the ES6 module is asynchronous loading. There is an independent resolution phase of module dependencies.
Synchronous loading: The so-called synchronous loading means that the process of loading resources or modules will block subsequent Code execution;
Asynchronous loading: will not block the execution of subsequent code;
Let’s look at a case and create the following directory;
| -- a.js | -- index.js | -- c.js
// a.js console.log('a.js文件的执行'); const importFun = () => { console.log(require('./c').c); } importFun() module.exports = { importFun }
// index.js const A = require('./a'); console.log('index.js的执行');
// c.js console.log('c.js的运行'); const c = 3 module.exports = { c }
Execute the command node index. js
// a.js文件的执行 // c.js的运行 // 3 // index.js的执行
We will find that the content of require will block the execution of subsequent code. Because c.js is printed first, and then index.js is printed, so require() is loaded synchronously;
// a.js console.log('a.js文件的执行'); export const importFun = () => { import('./c.js').then(({c})=>{ console.log(c) }) } importFun()
// index.js import {importFun} from './a.js' console.log('index.js的执行');
// c.js console.log('c.js的运行'); export const c = 3
// 结果 // a.js文件的执行 // index.js的执行 // c.js的运行 // 3
It can be seen that import() loads resources asynchronously, because c .js is printed after index.js and will not block the execution of subsequent code;
Summary: The above are the differences between commonjs and esmodule
1: The CommonJS module outputs a copy of the value, and the ES6 module outputs a reference to the value.
2: The CommonJS module is loaded at runtime, and the ES6 module is a compile-time output interface.
3: The require() of the CommonJS module loads the module synchronously, and the import command of the ES6 module loads asynchronously, with an independent parsing phase of module dependencies
[Related recommendations: javascript video tutorial 、webfrontend】
The above is the detailed content of What is the difference between es6 and commonjs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

In ES6, you can use the reverse() method of the array object to achieve array reversal. This method is used to reverse the order of the elements in the array, putting the last element first and the first element last. The syntax "array.reverse()". The reverse() method will modify the original array. If you do not want to modify it, you need to use it with the expansion operator "...", and the syntax is "[...array].reverse()".

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() to find the difference set, with the syntax " new Set([...newA].filter(x =>!newB.has(x)))", the difference set elements will be included in a set collection and returned; 3. Use Array.from to convert the set into an array Type, syntax "Array.from(collection)".

For browser compatibility. As a new specification for JS, ES6 adds a lot of new syntax and API. However, modern browsers do not have high support for the new features of ES6, so ES6 code needs to be converted to ES5 code. In the WeChat web developer tools, babel is used by default to convert the developer's ES6 syntax code into ES5 code that is well supported by all three terminals, helping developers solve development problems caused by different environments; only in the project Just configure and check the "ES6 to ES5" option.

No, require is the modular syntax of the CommonJS specification; and the modular syntax of the es6 specification is import. require is loaded at runtime, and import is loaded at compile time; require can be written anywhere in the code, import can only be written at the top of the file and cannot be used in conditional statements or function scopes; module attributes are introduced only when require is run. Therefore, the performance is relatively low. The properties of the module introduced during import compilation have slightly higher performance.

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

es2015 is es6. The full name of es is "ECMAScript", which is a universal scripting language implemented according to the ECMA-262 standard. The version officially released in June 2015 is officially called ECMAScript2015 (ES2015). Because it is the 6th version of ECMAScript, it can Referred to as es6.

The map is ordered. The map type in ES6 is an ordered list that stores many key-value pairs. The key names and corresponding values support all data types; the equivalence of key names is determined by calling the "Objext.is()" method. Implemented, so the number 5 and the string "5" will be judged as two types, and can appear in the program as two independent keys.
