module.exports在闭包中

时间:2013-01-21 13:17:13

标签: javascript node.js

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函数。

1 个答案:

答案 0 :(得分:1)

您可以使用:

<强> test.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");

<强> main.js

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