簡體   English   中英

閉包中的 module.exports

[英]module.exports in closures

var object = {}; // Global Object

(function() {

    var theArg, google, yahoo;

    object.google = function(arg) {
        theArg = arg;
        alert(theArg);
    }

    object.yahoo = function() {
        alert(theArg);
    }

    module.exports = yahoo;

})();

// This will set initial value of 
google("Hello World");

我可以調用這樣的東西module.exports = yahoo 並在其他地方調用yahoo函數。

您可以使用:

測試.js

var object = {}; // Global Object

alert = console.log;

(function() {

    var theArg, google, yahoo;

    object.google = function(arg) {
        theArg = arg;
        alert(theArg);
    }

    object.yahoo = function() {
        alert(theArg);
    }

    module.exports.yahoo = object.yahoo;

})();

// This will set initial value of 
object.google("Hello World");

主文件

require('./test.js').yahoo(); // Hello World

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM