What is the difference between commonjs and es6 modularity
Difference: 1. The CommonJS module is loaded at runtime, while the ES6 module is a compile-time output interface; 2. The require() of the CommonJS module loads the module synchronously, while the import command of the ES6 module loads asynchronously; 3. , CommonJS is a shallow copy of the module, and ES6 is the introduction of the module.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
CommonJS
For basic data types, it is a copy. That is, it will be cached by the module. At the same time, the variables output by this module can be reassigned in another module.
For complex data types, it is a shallow copy. Since the objects referenced by the two modules point to the same memory space, modifications to the value of the module will affect the other module.
When a module is loaded using the require command, the code of the entire module will be run.
When the require command is used to load the same module, the module will not be executed again, but the value in the cache will be obtained. In other words, no matter how many times the CommonJS module is loaded, it will only be run once when it is loaded for the first time. If it is loaded later, the result of the first run will be returned, unless the system cache is manually cleared.
When loading in a loop, it is executed during loading. That is, when the script code is require, it will all be executed. Once a module is "loop loaded", only the executed part will be output, and the unexecuted part will not be output.
ES6 module
The values in the ES6 module belong to [dynamic read-only reference].
For read-only, that is, the value of the imported variable is not allowed to be modified. The imported variable is read-only, whether it is a basic data type or a complex data type. When a module encounters an import command, a read-only reference is generated. When the script is actually executed, the value will be retrieved from the loaded module based on this read-only reference.
For dynamics, when the original value changes, the value loaded by import will also change. Whether it is a basic data type or a complex data type.
When loading in a loop, ES6 modules are dynamically referenced. As long as some reference exists between the two modules, the code will be able to execute.
The difference between ES6 modules and CommonJS modules
1. CommonJS modules are loaded at runtime, while ES6 modules are output interfaces at compile time.
2. The require() of the CommonJS module loads the module synchronously, while the import command of the ES6 module loads asynchronously, with an independent resolution phase for module dependencies.
3. CommonJS is a shallow copy of the module, and ES6 Module is the introduction of the module. That is, the ES6 Module is only read-only and cannot change its value. The specific point is that the pointer cannot be changed, similar to const.
4. The interface of import is read-only (read-only status), and its variable value cannot be modified. That is, the pointer pointing to its variable cannot be modified, but the internal pointer pointing to the variable can be changed. You can reassign a commonJS pair (change the pointer pointing), but assigning a value to an ES6 Module will result in a compilation error.
What ES6 modules and CommonJS modules have in common:
1. Both CommonJS and ES6 Modules can assign values to imported objects, that is, assign values to the internal properties of the objects. value is changed.
【Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What is the difference between commonjs and es6 modularity. 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.

How to Optimize the Maintainability of Java Code: Experience and Advice In the software development process, writing code with good maintainability is crucial. Maintainability means that code can be easily understood, modified, and extended without causing unexpected problems or additional effort. For Java developers, how to optimize the maintainability of code is an important issue. This article will share some experiences and suggestions to help Java developers improve the maintainability of their code. Following standardized naming rules can make the code more readable.

Python is a simple, easy-to-learn and efficient programming language, but when we write Python code, we may encounter some problems with excessive code complexity. If these problems are not solved, it will make the code difficult to maintain, error-prone, and reduce the readability and scalability of the code. So, in this article, we will discuss how to resolve code complexity error in Python code. Understanding Code Complexity Code complexity is a measure of the nature of code that is difficult to understand and maintain. In Python, there are some indicators that can be used

Python, as a high-level programming language, is widely used in software development. Although Python has many advantages, a problem that many Python programmers often face is that the maintainability of the code is poor. The maintainability of Python code includes the legibility, scalability, and reusability of the code. In this article, we will focus on how to solve the problem of poor maintainability of Python code. 1. Code readability Code readability refers to the readability of the code, which is the core of code maintainability.

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

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.

ES6 import will cause variable promotion. Variable hoisting is the promotion of a variable declaration to the very beginning of its scope. js has to go through the compilation and execution phases. During the compilation phase, all variable declarations will be collected and variables declared in advance, and other statements will not change their order. Therefore, during the compilation phase, the first step is already is executed, and the second part is executed only when the statement is executed in the execution phase.

In ES6, you can use the length attribute of the array object to determine how many items there are in the array, that is, to get the number of elements in the array; this attribute can return the number of elements in the array, just use the "array.length" statement. Returns a value representing the number of elements of the array object, that is, the length value.
