import和require的區別不用多說了,分屬於不同的規範,我們今天分別來試試怎麼使用它們
該規範通過exports
和module.exports
進行匯出,通過require
進行匯入,我們看下面兩個簡單的例子:
// b.js
module.exports =
// c.js
exports.cc =
'hello ccc'
// index.js
var bb =
require
('./b.js'
)var cc =
require
('./c.js'
)console.
log(bb)
// console.
log(cc)
//
既然exports
和module.exports
都可以匯出,那麼它倆什麼區別呢,其實node為每個模組提供乙個exports變數,指向module.exports,即:exports.name
和module.exports.name
一樣的,但是他倆其實也有細微差別,我們後面會講到。exports
和module.exports
的差別我先總結一下,後面再通過例子驗證:
exports
可以重複使用,匯出多個物件
module.exports
重複使用時,以最後乙個module.exports
為準
exports
和module.exports
一起使用時,exports
的物件都無法匯出
我們乙個乙個來驗證:
/* exports可以重複使用,匯出多個物件 */
// c.js
exports.aa =
'hello aa'
exports.bb =
'hello bb'
exports.cc =
'hello cc'
// index.js
var cc =
require
('./c.js'
)console.
log(cc)
//
/* module.exports重複使用時,以最後乙個module.exports為準 */
// c.js
module.exports =
module.exports =
// index.js
var cc =
require
('./c.js'
)console.
log(cc)
//
/* exports和module.exports一起使用時,exports的物件都無法匯出 */
// c.js
exports.aa =
'hello aaa'
module.exports =
exports.cc =
'hello ccc'
// index.js
var cc =
require
('./c.js'
)console.
log(cc)
//
我剛剛說過exports.name
和module.exports.name
一樣的,但是他倆其實也有細微差別,我們先說說他倆一樣的地方:
// c.js
exports.aa =
'hello aaa'
module.exports.bb =
'hello bbb'
exports.cc =
'hello ccc'
module.exports.dd =
'hello ddd'
// index.js
var cc =
require
('./c.js'
)console.
log(cc)
//
為了看它倆的差別,我們在中間插入乙個module.exports
看看:
// c.js
exports.aa =
'hello aaa'
module.exports.bb =
'hello bbb'
module.exports =
exports.cc =
'hello ccc'
module.exports.dd =
'hello ddd'
// index.js
var cc =
require
('./c.js'
)console.
log(cc)
//
可見,三種寫法都使用的時候,只有module.exports
和處於module.exports
下方的module.exports.dd
指向的物件可以匯出
es6使用export
匯出,使用import
匯入,我們先舉幾個簡單的例子:
// a.js
export
var aa =
110export
const bb =
'hello bb'
var cc =
2export
// index.js
import
*as all from
'./a.mjs'
console.
log(all)
// [module]
注意,export的實質是暴露介面,在介面名與模組內部變數之間,建立乙個一一對應的關係,所以以下的寫法是有問題的:
// 以下寫法均會報錯
var kkm =
1export kkm
export
'hello'
export
有一種情況也會報錯:
// 不報錯
var cc =
2export
// 報錯
var cc =
2export
as關鍵字
as關鍵字相當於給匯出和匯入的介面名起的別名,export
和import
均可以使用
// b.js
const bb =
'hello bb'
export
// index.js
import
*as all from
'./b.js'
console.
log(all)
//
// b.js
const bb =
'hello bb'
export
// index.js
import
from
'./b.js'
console.
log(bb)
// hello bb
default關鍵字
de****t
其實是一種簡寫的寫法,相當於將模組中乙個名為default的變數作為介面暴露出去,舉例子:
export
default54;
// 等價於
var dind =54;
export
;
因此,default只能export一次,如果多次export,則會報錯duplicate export of 'default'
export default
對應的import寫法:
import bb from
'./b.js'
// 等價於
import
from
'./b.js'
和commonjs規範不同,export
暴露的是介面,可以認為是指標,即export
的變數發生變化時,import
的變數也會發生變化,我們來測試一下:
// b.js
var dind =54;
// 變化
settimeout((
)=>
,2000
)export
;
import bb from
'./b.js'
setinterval((
)=>
,500
);
index.js
執行結果:
54
5454
change
change
change
...
import和require的區別
node程式設計中最重要的思想就是模組化,import和require都是被模組化所使用。遵循規範 呼叫時間 本質 require exports 遵循 commonjs amd,只能在執行時確定模組的依賴關係及輸入 輸出的變數,無法進行靜態優化。用法只有以下三種簡單的寫法 const fs req...
import和require的區別
遵循規範 require 是 amd規範引入方式 import是es6的乙個語法標準,如果要相容瀏覽器的話必須轉化成es5的語法 呼叫時間 require是執行時呼叫,所以require理論上可以運用在 的任何地方 import是編譯時呼叫,所以必須放在檔案開頭 本質require是賦值過程,其實r...
import和require的區別
node程式設計中最重要的思想就是模組化,import和require都是被模組化所使用。遵循規範 require 是 amd規範引入方式 import是es6的乙個語法標準,如果要相容瀏覽器的話必須轉化成es5的語法呼叫時間 require是執行時呼叫,所以require理論上可以運用在 的任何地...