Is it possible to do something like this?
function one(){
two(arguments)
}
function two(a, b){
console.log(a);
console.log(b);
}
one('a', 'b');
Yes, use apply()
function one(){
two.apply(null, [].slice.call(arguments))
}
Some older engines insist upon receiving an array as the second parameter to apply(). Unfortunately, arguments is an array-like object. That's to say, it's not truly an array but it has a length property and numeric indices in addition to callee and caller properties. So, we call Array.slice() in order to get a plain array to pass to apply().
That said, V8 (used by node.js) should be fine without such translation:
function one(){
two.apply(null, arguments)
}
That was my fault, I missed the node.js tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With