hyperpolyglot icon indicating copy to clipboard operation
hyperpolyglot copied to clipboard

There is a way to pass array elements as separate arguments in Node.js

Open Finesse opened this issue 9 years ago • 3 comments

var a = [1, 2, 3];
add3.apply(window, a);

Finesse avatar Nov 28 '15 04:11 Finesse

I don't think node has a global window object. I get "ReferenceError: window is not defined" when I try it.

clarkgrubb avatar Apr 04 '16 02:04 clarkgrubb

@clarkgrubb global works also.

Also try using this instead.

var a = [1, 2, 3];
add3.apply(this, a);

If you are nesting the functions you could set window=this or global=this at the beginning of the file

7fe avatar Apr 04 '16 19:04 7fe

As this function doesn't need a context call, you can just:

add3.apply(null, a)

hbarcelos avatar Jun 15 '16 15:06 hbarcelos