Export and export default are the syntax for variables in export modules in ES6

Exports and module.exports are syntax for variables in exported modules in Nodejs (based on CommonJs syntax specification)

[export] — Name the export

When you create a JavaScript module, the export statement is used to export real-time bound functions, objects, or raw values from the module so that other programs can use them through the import statement. The exported binding value can still be modified locally. When importing using import, these binding values can only be read by the import module, but when modifying these binding values in the export module, the modified values are also updated in real time.

[syntax]

  1. Named exports (any number per module)

  2. Default export (one per module)

    // Export a single feature
    export letName1 name2,... , nameN;// also var, const
    export letName1 =... , name2 =... ,... , nameN;// also var, const
    export function FunctionName(){... }export class ClassName {... }// Export the list
    export { name1, name2, …, nameN };
    
    // Rename the export
    export { variable1 as name1, variable2 asName2,... , nameN };// Deconstruct export and rename
    export const { name1, name2: bar } = o;
    
    // Export by default
    export default expression;
    export default function (...) {... }// also class, function*
    export default function name1(...) {... }// also class, function*
    export { name1 as default,... };// Export a collection of modules
    export * from... ;// does not set the default export
    export * as name1 from... ;/ / Draft ECMAScript ® 2 o21
    export { name1, name2, …, nameN } from... ;export { import1 as name1, import2 asName2,... , nameN }from... ;export { default } from... ;Copy the code

[export default] — Export by default

Two different export methods are named export and export default. It is possible to define multiple named exports per module, but only one default export is allowed. Each method corresponds to one of the above syntax:

Named export:

// Export predefined features
export{myFunction, myVariable};// Export a single feature (var, let,
/ / const, function, class)
export let myVariable = Math.sqrt(2);
export function myFunction() {... };Copy the code

Default export:

// Export predefined features as default values
export { myFunction as default };

// Export a single feature as default
export default function () {... }export default class {.. }// Each export overwrites the previous export
Copy the code

Named exports are useful when exporting multiple values. During import, the same name of the corresponding object must be used.

However, the default export can be imported using any name, for example:

/ / file test. Js
let k; 
/ / export
export default k = 12; 
Copy the code
// Another file
import m from './test'; // Since k is the default export, you can freely use import m instead of import k
console.log(m);        // Output is 12
Copy the code

You can also rename the export to avoid naming conflicts:

export { 
		myFunction as function1,
    myVariable as variable 
};
Copy the code

“In column”

1. Name the export — column 1

//lib.js
// Export constants
export const sqrt = Math.sqrt;
// Export the function
export function square(x) {
    return x * x;
}
// Export the function
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
Copy the code
//main.js
import { square, diag } from './lib';
console.log(square(11)); / / 121
console.log(diag(4.3)); / / 5
Copy the code

2. Name the export — column 2

// module "my-module.js"
function cube(x) {
  return x * x * x;
}

const foo = Math.PI + Math.SQRT2;

var graph = {
    options: {
        color:'white'.thickness:'2px'
    },
    draw: function() {
        console.log('From graph draw function'); }}export { cube, foo, graph };
Copy the code
import { cube, foo, graph } from 'my-module.js';

graph.options = {
    color:'blue'.thickness:'3px'
};

graph.draw();
console.log(cube(3)); / / 27
console.log(foo);    / / 4.555806215962888
Copy the code

3. Export by default — column 3

// module "my-module.js"
export default function cube(x) {
  return x * x * x;
}
Copy the code
import cube from './my-module.js';
console.log(cube(3)); / / 27
Copy the code

4. Export by default — column 4

// module "my-module.js"
function handlerHexDisplay(data) {
    return data;
}

function sendCommand(address,command) {
    return address+command;
}

export default {sendCommand , openCom}
Copy the code
import cube from './my-module.js';
cosole.log(cube.sendCommand(1.2))
cosole.log(cube.openCom(10))
Copy the code

【 module. Exports 】

Node applications are composed of modules that follow the CommonJS module specification. According to this specification, each file is a module with its own scope. Variables, functions, and classes defined in one file are private and invisible to other files. The CommonJS specification states that within each module, the module variable represents the current module. This variable is an object whose exports property (module.exports) is the interface to the outside world. Loading a module loads the module.exports property of that module.

function clear() {
  uni.clearStorageSync();
}
module.exports = {
      clear:clear,
}
Copy the code

Module. exports exports function clear

var example = require('./example.js'); // Import method 1
import example from './example.js'// Import method 2
console.log(example.x);
Copy the code

“In column”

  1. Column 1
// common.js
function functA() {
  console.log('1')}function functB() {
  console.log('2')}function functC() {
  console.log('3')}module.exports = {
      functA:functA,
      functB:functB,
      functC:functC
}
Copy the code
import common from '@/utils/common.js';
console.log(common.functA);
console.log(common.functB);
console.log(common.functC);
Copy the code
  1. Column 2 – Returns a JSON Object
var app = {
    name: 'app'.version: '1.0.0'.sayName: function(name){
        console.log(this.name); }}module.exports = app;
Copy the code

This method can return globally shared variables or methods. Call method:

var app = require('./app.js');
app.sayName('hello');//hello
Copy the code
  1. Column 3 – Returns a constructor

CLASS.js:

var CLASS = function(args){
     this.args = args;
}
module.exports = CLASS;
Copy the code

Call:

var CLASS = require('./CLASS.js');
varc = new CLASS('arguments');
Copy the code
  1. Column 3 – Returns an instance object
//CLASS.js
var CLASS = function(){
    this.name = "class";
}
CLASS .prototype.func = function(){
    alert(this.name);
}
module.exports = new CLASS();
Copy the code

Call:

var c = require('./CLASS.js');
c.func();//"class"
Copy the code