Before es6 js has not its own grammar module, in order to solve this embarrassing there is the emergence of require.js. After es6 js release and the introduction of import of the concept makes clear distinction between the two students create their own misunderstanding in actual use, after access to relevant information in their own little insight into this record.
Core concepts: defining exported file module.export, object type deriving not defined (may be any type, string variables, objects, method), the file calling require introduced () method of introducing the object i.e. can.
//a.js in
module.export = {
a: function(){
console.log(666)
}
}
//b.js in
var obj = require('../a.js')
obj.a() //666
[Note]: Essentially the object is to be exported to the module assignment export attribute this object, require access to the property by this method in other documents
//a.js in
export default {// (most commonly used methods, add the keyword default representatives can use any variable name at the time of import and does not require braces {})
a: function(){
console.log(666)
}
}
export function () {// derivation function
}
export {newA as a, b, c} // deconstruction assignment syntax (as keywords here shows a newA as a data interface exposed to the outside, can not directly access the external a)
//b.js in
import a from '...' // import common grammar (with the need to export the default keyword) can specify any name import of
import {...} from '...' // basic embodiment, the objects need to be introduced with the destructuring assignment export objects.
import a as biubiubiu from '...' // use as a keyword, represented here will be a representative of biubiubiu introduced (when the variable name can be used in this way to resolve conflicts when there is a conflict)
import {a as biubiubiu, b, c} // as use of other keywords
The difference between them
var a = require(a() + '/ab.js')
Require performance relative to import slightly lower, because require the introduction of modules at runtime and also assigned to a variable, while import only need to import the module interfaces introduced so specified at compile time based on performance slightly higher
var a = 6
export default {a}
a = 7 // export may in es6
var a = 6
module.export = a
a = 7 // in the common.js, this is wrong
Usage require / exports only three simple wording:
const fs = require('fs')
exports.fs = fs
module.exports = fs
The import / export of writing on varied:
import fs from 'fs'
import {default as fs} from 'fs'
import * as fs from 'fs'
import {readFile} from 'fs'
import {readFile as read} from 'fs'
import fs, {readFile} from 'fs'
export default fs
export const fs
export function readFile
export {readFile, read}
export * from 'fs'
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...
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...
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...
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...
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...
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...
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...
require () / import command / import () function is the difference between: require a loading operation, that is dynamically loaded. The import command is a static load, load at compile time. import (...