JS 之export、export default和module.exports

exportexport default是ES6中导出模块中变量的语法

exportsmodule.exports是Nodejs中导出模块中变量的语法(基于CommonJs语法规范)

【export】-- 命名导出

在创建JavaScript模块时,export 语句用于从模块中导出实时绑定的函数、对象或原始值,以便其他程序可以通过 import 语句使用它们。被导出的绑定值依然可以在本地进行修改。在使用import进行导入时,这些绑定值只能被导入模块所读取,但在export导出模块中对这些绑定值进行修改,所修改的值也会实时地更新。

【语法】

  1. 命名导出(每个模块包含任意数量)

  2. 默认导出(每个模块包含一个)

    // 导出单个特性
    export let name1, name2, …, nameN; // also var, const
    export let name1 = …, name2 = …, …, nameN; // also var, const
    export function FunctionName(){...}
    export class ClassName {...}
    
    // 导出列表
    export { name1, name2, …, nameN };
    
    // 重命名导出
    export { variable1 as name1, variable2 as name2, …, nameN };
    
    // 解构导出并重命名
    export const { name1, name2: bar } = o;
    
    // 默认导出
    export default expression;
    export default function (…) { … } // also class, function*
    export default function name1(…) { … } // also class, function*
    export { name1 as default, … };
    
    // 导出模块合集
    export * from …; // does not set the default export
    export * as name1 from …; // Draft ECMAScript® 2O21
    export { name1, name2, …, nameN } from …;
    export { import1 as name1, import2 as name2, …, nameN } from …;
    export { default } from …;
    

【export default】 -- 默认导出

两种不同的导出方式,命名导出(export)和默认导出(export default)。能够在每一个模块中定义多个命名导出,但是只允许有一个默认导出。每种方式对应于上述的一种语法:

命名导出:

// 导出事先定义的特性
export { myFunction,myVariable };

// 导出单个特性(可以导出var,let,
//const,function,class)
export let myVariable = Math.sqrt(2);
export function myFunction() { ... };

默认导出:

// 导出事先定义的特性作为默认值
export { myFunction as default };

// 导出单个特性作为默认值
export default function () { ... }
export default class { .. }

// 每个导出都覆盖前一个导出

在导出多个值时,命名导出非常有用。在导入期间,必须使用相应对象的相同名称。

但是,可以使用任何名称导入默认导出,例如:

// 文件 test.js
let k; 
// 导出
export default k = 12; 
// 另一个文件
import m from './test'; // 由于 k 是默认导出,所以可以自由使用 import m 替代 import k
console.log(m);        // 输出为 12 

也可以重命名命名导出以避免命名冲突:

export { 
        myFunction as function1,
    myVariable as variable 
};

【示列】

1.命名导出 -- 列1

//lib.js
//导出常量
export const sqrt = Math.sqrt;
//导出函数
export function square(x) {
    return x * x;
}
//导出函数
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
//main.js
import { square, diag } from './lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5

2.命名导出 -- 列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 };
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

3.默认导出 -- 列3

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

4.默认导出 -- 列4

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

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

export default {sendCommand , openCom}
import cube from './my-module.js';
cosole.log(cube.sendCommand(1,2))
cosole.log(cube.openCom(10))

【module.exports】

Node应用由模块组成,采用CommonJS模块规范。根据这个规范,每个文件就是一个模块,有自己的作用域。在一个文件里面定义的变量、函数、类,都是私有的,对其他文件不可见。CommonJS规范规定,每个模块内部,module变量代表当前模块。这个变量是一个对象,它的exports属性(即module.exports)是对外的接口。加载某个模块,其实是加载该模块的module.exports属性。

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

上面代码通过module.exports输出函数 clear

var example = require('./example.js'); // 导入方法一
import example from './example.js'// 导入方法二
console.log(example.x);

【示列】

  1. 列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
}
import common from '@/utils/common.js';
console.log(common.functA);
console.log(common.functB);
console.log(common.functC);
  1. 列2 - 返回一个JSON Object
var app = {
    name: 'app',
    version: '1.0.0',
    sayName: function(name){
        console.log(this.name);
    }
}
module.exports = app;

这种方法可以返回全局共享的变量或者方法。
调用方法:

var app = require('./app.js');
app.sayName('hello');//hello
  1. 列3 - 返回一个构造函数

CLASS.js:

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

调用:

var CLASS = require('./CLASS.js');
varc = new CLASS('arguments');
  1. 列3 - 返回一个实例对象
//CLASS.js
var CLASS = function(){
    this.name = "class";
}
CLASS .prototype.func = function(){
    alert(this.name);
}
module.exports = new CLASS();

调用:

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

你可能感兴趣的:(JS 之export、export default和module.exports)