Javascript 模块.闭包中的导出

Javascript 模块.闭包中的导出,javascript,node.js,Javascript,Node.js,我可以这样称呼吗module.exports=yahoo;并在别处调用yahoo函数。您可以使用: test.js var object = {}; // Global Object (function() { var theArg, google, yahoo; object.google = function(arg) { theArg = arg; alert(theArg); } object.yahoo = fun

我可以这样称呼吗
module.exports=yahoo
;并在别处调用
yahoo
函数。

您可以使用:

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

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");
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");
您可以使用:

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

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");
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");

我不明白。你如何才能调用
谷歌
?它是
对象
的成员方法
google
本身超出了范围。我不明白这一点。你如何才能调用
谷歌
?它是
对象
的成员方法
google
本身不在范围之内。但是当我调用object.yahoo时,我没有收到警报。你在哪里调用它?在这个文件中,或者在您需要this的其他文件中?我是从不同的文件调用的。如果您直接使用alert,您将得到缺少函数的错误。改用
console.log
但是当我调用object.yahoo时,我没有收到警报。你在哪里调用它?在这个文件中,或者在您需要this的其他文件中?我是从不同的文件调用的。如果您直接使用alert,您将得到缺少函数的错误。改用
console.log