phillro/node-elasticsearch-client

Manipulation of arguments array fails

algesten opened this issue · 1 comments

I pulled this out of core.js (and added console.log)

//Pull the callback and set it false to not clobber id.
if(typeof arguments[arguments.length-1]=='function'){
    console.log('yes');
    cb=arguments[arguments.length-1];
    console.log(cb);
    arguments[arguments.length-1]=false;
    console.log(cb);
}

When running via coffee-script, the callback stopped working. And I found this:

yes
[Function]
false

Notice how the cb variable changes value when the arguments array is changed. Why I have no idea – but I would personally avoid manipulating arguments.

For kicks I tested this:

var args = Array.prototype.slice.call(arguments, 0);

//Pull the callback and set it false to not clobber id.
if(typeof args[args.length-1]=='function'){
    console.log('yes');
    cb=args[args.length-1];
    console.log(cb);
    args[args.length-1]=false;
    console.log(cb);
}

And it produced the expected

yes
[Function]
[Function]

yep me too.
works if I make a new local var to store the cb.

what's the rationale for manipulating arguments anyway?
if there's a good reason, praps your 'just for kicks' rewrite should still set
arguments[arguments.length-1]=false;
(just using the sliced copy for safely remembering the cb)