2011-05-09 87 views

回答

4

exports = handle

這將創建一個名爲exports一個局部變量。這與覆蓋module.exports不同。

module.exports = handle

這將覆蓋出口變量,居住在模塊範圍內,這就要通過require讀取。

在瀏覽器window["foo"]foo是相同的,但在節點module["foo"]foo行爲微妙不同。

本地變量作用域上下文和module不是同一回事。

1

做:

function handle(msg) { 
    .... 
} 
module.exports = handle; 

和它的作品,你想要的方式。

+1

或module.exports = function(msg){} – Tom 2011-05-09 16:18:10

0

exports VS module.exports VS exports.something)這背後的問題的問題在這篇文章中是最好的描述:

http://www.alistapart.com/articles/getoutbindingsituations

第一個版本(exports = handle)是完全的問題:缺少的綁定是強制性的的javascript:

exports = handle意味着window.exports = handle(或任何的node.js具有與全局對象)

0

看到問題的另一個辦法就是想着節點可以如何加載模塊:

function loadModule(module, exports) {

裏面來了你的模塊代碼

}

如果你的代碼覆蓋exports參數( exports = handle),這個改變從這個函數的外部是不可見的。而對於這種覆蓋,可以使用module對象。

如果導出將是函數體所在作用域中可見的變量,則不會發生此問題。

相關問題