javascript - Require to global scope in node.js - Stack Overflow

In my node.js script I have:var wu = require('.wu-0.1.8.js');just to play with wu.If it was

In my node.js script I have:

var wu = require('./wu-0.1.8.js');

just to play with wu.

If it was in browser, then I could just use wu everywhere e.g.

wu([1,2,3]).map( function(n){ return n*n; } );

Hovever in node.js I have to write:

wu.wu([1,2,3]).map( function(n){ return n*n; } );

Is it possible to append wu to global scope so that I dont have to type wu.wu all the time?

In my node.js script I have:

var wu = require('./wu-0.1.8.js');

just to play with wu.

If it was in browser, then I could just use wu everywhere e.g.

wu([1,2,3]).map( function(n){ return n*n; } );

Hovever in node.js I have to write:

wu.wu([1,2,3]).map( function(n){ return n*n; } );

Is it possible to append wu to global scope so that I dont have to type wu.wu all the time?

Share Improve this question asked Oct 1, 2013 at 18:53 Sir BohumilSir Bohumil 2412 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6
var wu = require('./wu-0.1.8.js').wu;

edit (response to ments):

If you need to use other methods in require('./wu-0.1.8.js'), you can always do this...

var wuModule = require('./wu-0.1.8.js'); 
var wu = wuModule.wu;

// Now you can do 

wu([1,2,3]).map( function(n){ return n*n; } );
wuModule.someOtherWuMethod(...);

Well, I suggest you not to do so but you can iterate through the wu object properties and attach them to global object:

var _wu = require('./wu-0.1.8.js');
var key;
for (key in _wu) {
  if (_wu.hasOwnProperty(key)) {
    global[key] = _wu[key];
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745119596a4612333.html

相关推荐

  • javascript - Require to global scope in node.js - Stack Overflow

    In my node.js script I have:var wu = require('.wu-0.1.8.js');just to play with wu.If it was

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信