简体   繁体   中英

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

Can i call something like this module.exports = yahoo ; and calling the yahoo function else where.

You can use:

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(); // Hello World

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM