3

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?

2 Answers 2

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

edit (response to comments):

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(...);
Sign up to request clarification or add additional context in comments.

3 Comments

what if there were more exported functions that I wanted to access, say 100 ?
you mean like, wu.foo(), wu.bar(), in addition to wu.wu()?
Then my answer would not work... but it wouldn't make sense for there to be a wu.wu method at all if it's a "master" function (like $ is in jQuery). The designer(s) of wu should have exported the wu function and added wu.foo and wu.bar as methods (properties of wu).
3

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];
  }
}

2 Comments

Cool... but so broken!
@wprl what is so broken?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.