Javascript (es2016) import and require usage and difference

Write a simple js file, assuming the name is: lib.js . The assumptions are as follows:

export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}

This way you can reference the properties and methods defined in lib in other places. There are two ways to reference them, namely import and require.

//method one
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3));
//method
import * as lib from 'lib';
square = lib.square;

You can also set the default export information, you need to define export default {} in lib.js. Default can be followed by a parameter, or an array. The writing method is:

//------ module1.js ------
export default 123;

//------ module2.js ------
const D = 123;
export { D as default };

Usually used to the first one. Then use import to get the array or parameter. But import can only be used for static import, it must be written at the top of the file at the beginning of the file. And require can achieve dynamic loading.
[table]
|Loading Method|Specification|Command|Features|
|Runtime Loading|CommonJS/AMD| require | Community Program, which provides a server/browser module loading scenario. Non-verbal level standards. Module dependencies and input/output variables can only be determined at runtime and cannot be statically optimized. |
|Compile-time loading|ESMAScript6+| import | Language specification level support module functionality. Support compile-time static analysis, which is convenient for JS to introduce macro and type checking. Dynamic binding. |
[/table]

const incrementCounter = function ({dispatch,state}){
dispatch(‘INCREMENT‘)
}
export default {
incrementCounter
}
//require
let myAction = require(‘xxxxx‘);
myAction.default.incrementCounter()

Intelligent Recommendation

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

require and import difference in JS

Both are aimed at JS modular programming. Follow the norms requireAMD is introducing standardized way importThe syntax is a standard es6, if you want a compatible browser then be converted into the sy...

The difference between require and import of

Follow the norms -require AMD is introducing standardized way -import is a standard grammar es6, if you want a compatible browser then be converted into the syntax es5   Call time -require is to ...

import and require the use of difference

Herein refers to the use of the JS, although other languages ​​also introduced (e.g., Python) First of all: is the difference between the two: import ES6 is utilized modular, and used And the require,...

Import and Require difference

Follow the specification Require is an AMD specification introduction method Import is a grammar standard for ES6. If you want to compatibility with the browser, you must convert to ES5 syntax. Call t...

More Recommendation

Difference between IMPORT and REQUIRE

Why is ES6 to add import? In the past, we will userequire()Function to introduce functions or code from external files or modules. At this time, I will encounter a problem: some files or modules will ...

Difference between require and import

#require and import difference The most important idea in node programming is modularity, and import and require are both used by modularity. In ES6, use export to export the interface and import to i...

Require and import in javascript

Modular code is widely adopted best practice in program development in various languages Increase the legibility of the code. Generally, the code of more than 200 lines is very difficult to read. Impr...

The difference between require and import in 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?

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

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

Top