(Transfer) The difference between import and require

Before ES6, there was a bill to load JS modules, the most important being CommonJS and AMD specifications. The former CommonJS is mainly used in servers to achieve synchronous loading, such as nodejs. The AMD specification is applied to browsers, such as requirejs, for asynchronous loading.

The difference between import and require

The most important idea in node programming is modularity. Import and require are both modularized.

Require is called at runtime, so it can be imported everywhere

Import is called at compile time and must be imported at the beginning of the file. Currently, some browsers do not support it. You need to use babel to convert es6 to es5 and then execute

 

import (es6 syntax)

ES6 modules mainly have two functions: export and import

export is used to output the interface of this module (a file can be understood as a module) variables

import is used to load the export output variable interface in a module

Such as the following code:

 

,,,,,,, Variable

 

 

,,,,,, Import variables

 

require (CommandJS specification, used in nodejs)

In the nodejs environment, we use the CommandJS module specification, use require to import modules, and use module.exports to export interfaces

Such as the following code:

 

,,,, Derive the variables

 

,,, Derive



OF: jinya2437
Link: https://www.jianshu.com/p/4061d77128e6
Source: Jianshu
The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Intelligent Recommendation

Analysis of the difference between import and require

1. require is the AMD specification, and import is the ES6 module. 2. require similar way of writing It seems to only introduce a, b, c, actually introduce the whole module, and then assign the corres...

[js] difference between require and import

Article directory The difference between `require` and `import` origin Export command / import command CommonJS es6 Static optimization requirewithimportDifference origin Before the birth of es6, js h...

The difference between require and import in vue

The difference between require and import Before es6, js has not had its own module syntax. In order to solve this problem, there is a requirement for require.js. After the release of es6, js introduc...

The difference between the import and require the webpack

Foreword Some time ago wrote an articleimport and require Depth - depth from shallow thinking to bring NodeThe articles comeimportis based onrequireImplementation, and implementation is different in d...

The difference between modular require and import

When the front-end application becomes more and more complex, we want to divide the code into different modules for easy reuse and on-demand loading. Require and import are statements that introduce m...

More Recommendation

The difference between the Require and Import of Vue

First we have to understand the basic syntax of Require and Import: REQUIRE's basic syntax: Define module.export in the exported file, the type of object to export is not limited (which can be any typ...

The difference between import module require and import in node

Should I use require or import? The use of require is very simple, it is equivalent to the portal of module.exports, what is behind module.exports, what is the result of require, objects, numbers, str...

What is the difference between js using require and import?

Both require and import are used for js modularization. First, require Require is the specification of commonjs. The node application is composed of modules and complies with the specification of comm...

The difference between require/exports and import/export

Require/exports appears first. It is produced in the specifications drafted by the developers of the js community and is widely recognized or widely used. Import/export is ES6. CommonJS is a specifica...

The difference between the role and the scope chain and require the import

import is a reference require a copy   In theory, the value of reference require changes will no longer be referenced files inside the affected, and this indeed is the case, That way the value of...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top