The difference between import and require the

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.


  • The basic syntax require the

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

  • Import of basic grammar
    core concepts: values ​​derived object must correspond with the modules, is another wayExported objects with the entire structure assignment module. Right, you heard it right. Seize the key, deconstruction assignment! ! ! ! !
//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

  • require the assignment process is executed and is only running, import and deconstruction process is executed at compile time. require can be understood as a global method, it can even perform such operations show below, is a method means you can perform anywhere. The import must be written at the top of the file.
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

  • Exported after module.export in commom.js value can no longer be changed, but es6 the export is possible.
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

require / exports and import / export form is not the same

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'

 

Intelligent Recommendation

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...

(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 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...

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...

More Recommendation

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...

About the difference between the module and require the import

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 (...

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

Top