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 introduced the concept of import so that students who are not clear about the difference between the two have caused their own misunderstandings in the actual use process. After reviewing the relevant materials, they recorded their own small opinions.
Core concept: define module.export in the exported file, the type of the exported object is not limited (can be any type, string, variable, object, method), call the require() method in the imported file to introduce the object can.
//a.js
module.export = {
a: function(){
console.log(666)
}
}
//b.js
var obj = require('../a.js')
obj.a() //666
[Note]: Essentially, the object to be exported is assigned to the export attribute of the object of module. In other files, the attribute is accessed by the require method.
//a.js
Export default{ // (The most commonly used method, adding the default keyword means that you can use any variable name in import and don't need curly braces {})
a: function(){
console.log(666)
}
}
Export function(){ //export function
}
Export {newA as a ,b,c} // Deconstructing the assignment syntax (as keyword indicates that the data interface that exposes newA as a is exposed to the outside, and the external cannot directly access a)
//b.js
Import a from '...' //import common syntax (requires export with default keyword) can arbitrarily specify the name of import
Import {...} from '...' // Basically, the imported object needs to be destructured with the export object.
Import a as biubiubiu from '...' //Use the as keyword, which means that a represents biubiubiu (this can be used to resolve conflicts when variable names have conflicts)
Import {a as biubiubiu,b,c} //Other use of the as keyword
The difference between them
var a = require(a() + '/ab.js')
The performance of require is slightly lower than that of import, because require is introduced at runtime and is also assigned to a variable, and import only needs to import the specified module at compile time according to the interface in import, so the performance is slightly higher.
var a = 6
export default {a}
a = 7 //export in es6 can
var a = 6
module.export = a
a = 7 //This is wrong in common.js
The difference between require and import in JS Both of these are used for JS modular programming. Follow the specification requireIs the introduction method of AMD specification importIs a grammar st...
What is the difference between require and import? Different specifications to follow 1.require/exports is part of CommonJS 2.import/export is the new ES6 specification Different time of appearance Co...
When modifying vue&react and webpack, I often see require and jload in js file, both of which are used for JS modular programming (CSS is @import). Let's look at the difference between them. where...
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...
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...
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...
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....
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...
Author: Chi-inch link: https://www.zhihu.com/question/56820346/answer/150724784 Source: Knowing Copyright is owned by the author. For commercial reprint, please contact the author for authorization. F...
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...