繁体   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