export和export default是ES6中導(dǎo)出模塊中變量的語(yǔ)法
exports和module.exports是Nodejs中導(dǎo)出模塊中變量的語(yǔ)法(基于CommonJs語(yǔ)法規(guī)范)
【export】-- 命名導(dǎo)出
在創(chuàng)建JavaScript模塊時(shí),export 語(yǔ)句用于從模塊中導(dǎo)出實(shí)時(shí)綁定的函數(shù)、對(duì)象或原始值,以便其他程序可以通過(guò) import 語(yǔ)句使用它們。被導(dǎo)出的綁定值依然可以在本地進(jìn)行修改。在使用import進(jìn)行導(dǎo)入時(shí),這些綁定值只能被導(dǎo)入模塊所讀取,但在export導(dǎo)出模塊中對(duì)這些綁定值進(jìn)行修改,所修改的值也會(huì)實(shí)時(shí)地更新。
【語(yǔ)法】
命名導(dǎo)出(每個(gè)模塊包含任意數(shù)量)
-
默認(rèn)導(dǎo)出(每個(gè)模塊包含一個(gè))
// 導(dǎo)出單個(gè)特性 export let name1, name2, …, nameN; // also var, const export let name1 = …, name2 = …, …, nameN; // also var, const export function FunctionName(){...} export class ClassName {...} // 導(dǎo)出列表 export { name1, name2, …, nameN }; // 重命名導(dǎo)出 export { variable1 as name1, variable2 as name2, …, nameN }; // 解構(gòu)導(dǎo)出并重命名 export const { name1, name2: bar } = o; // 默認(rèn)導(dǎo)出 export default expression; export default function (…) { … } // also class, function* export default function name1(…) { … } // also class, function* export { name1 as default, … }; // 導(dǎo)出模塊合集 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】 -- 默認(rèn)導(dǎo)出
兩種不同的導(dǎo)出方式,命名導(dǎo)出(export)和默認(rèn)導(dǎo)出(export default)。能夠在每一個(gè)模塊中定義多個(gè)命名導(dǎo)出,但是只允許有一個(gè)默認(rèn)導(dǎo)出。每種方式對(duì)應(yīng)于上述的一種語(yǔ)法:
命名導(dǎo)出:
// 導(dǎo)出事先定義的特性
export { myFunction,myVariable };
// 導(dǎo)出單個(gè)特性(可以導(dǎo)出var,let,
//const,function,class)
export let myVariable = Math.sqrt(2);
export function myFunction() { ... };
默認(rèn)導(dǎo)出:
// 導(dǎo)出事先定義的特性作為默認(rèn)值
export { myFunction as default };
// 導(dǎo)出單個(gè)特性作為默認(rèn)值
export default function () { ... }
export default class { .. }
// 每個(gè)導(dǎo)出都覆蓋前一個(gè)導(dǎo)出
在導(dǎo)出多個(gè)值時(shí),命名導(dǎo)出非常有用。在導(dǎo)入期間,必須使用相應(yīng)對(duì)象的相同名稱(chēng)。
但是,可以使用任何名稱(chēng)導(dǎo)入默認(rèn)導(dǎo)出,例如:
// 文件 test.js
let k;
// 導(dǎo)出
export default k = 12;
// 另一個(gè)文件
import m from './test'; // 由于 k 是默認(rèn)導(dǎo)出,所以可以自由使用 import m 替代 import k
console.log(m); // 輸出為 12
也可以重命名命名導(dǎo)出以避免命名沖突:
export {
myFunction as function1,
myVariable as variable
};
【示列】
1.命名導(dǎo)出 -- 列1
//lib.js
//導(dǎo)出常量
export const sqrt = Math.sqrt;
//導(dǎo)出函數(shù)
export function square(x) {
return x * x;
}
//導(dǎo)出函數(shù)
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.命名導(dǎo)出 -- 列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.默認(rèn)導(dǎo)出 -- 列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.默認(rèn)導(dǎo)出 -- 列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應(yīng)用由模塊組成,采用CommonJS模塊規(guī)范。根據(jù)這個(gè)規(guī)范,每個(gè)文件就是一個(gè)模塊,有自己的作用域。在一個(gè)文件里面定義的變量、函數(shù)、類(lèi),都是私有的,對(duì)其他文件不可見(jiàn)。CommonJS規(guī)范規(guī)定,每個(gè)模塊內(nèi)部,module變量代表當(dāng)前模塊。這個(gè)變量是一個(gè)對(duì)象,它的exports屬性(即module.exports)是對(duì)外的接口。加載某個(gè)模塊,其實(shí)是加載該模塊的module.exports屬性。
function clear() {
uni.clearStorageSync();
}
module.exports = {
clear:clear,
}
上面代碼通過(guò)module.exports輸出函數(shù) clear
var example = require('./example.js'); // 導(dǎo)入方法一
import example from './example.js'// 導(dǎo)入方法二
console.log(example.x);
【示列】
- 列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);
- 列2 - 返回一個(gè)JSON Object
var app = {
name: 'app',
version: '1.0.0',
sayName: function(name){
console.log(this.name);
}
}
module.exports = app;
這種方法可以返回全局共享的變量或者方法。
調(diào)用方法:
var app = require('./app.js');
app.sayName('hello');//hello
- 列3 - 返回一個(gè)構(gòu)造函數(shù)
CLASS.js:
var CLASS = function(args){
this.args = args;
}
module.exports = CLASS;
調(diào)用:
var CLASS = require('./CLASS.js');
varc = new CLASS('arguments');
- 列3 - 返回一個(gè)實(shí)例對(duì)象
//CLASS.js
var CLASS = function(){
this.name = "class";
}
CLASS .prototype.func = function(){
alert(this.name);
}
module.exports = new CLASS();
調(diào)用:
var c = require('./CLASS.js');
c.func();//"class"