Friday, 15 February 2013

node.js - Appending to agrument object in javascript -



node.js - Appending to agrument object in javascript -

the arguments variable returns object like:

console.log(arguments) => { '0': 'arg1', '1': function[] }

what if wanted append , shift right?

{ '0': 'add', '1': 'arg1', '2': function[] }

how can accomplish this? manually or underscorejs

useful info underscore:

oarray_.toarray(list)

creates real array list (anything can iterated over). useful transmuting arguments object.

did like:

var args = _.toarray(arguments) args.unshift(fn)

you can apply array unshift method on arguments object:

array.prototype.unshift.call(arguments, "add");

however, it's improve not mess arguments object. rather convert array, prepend "add" that, , utilize instead of arguments in function body:

var args = ["add"].concat(array.prototype.slice.call(arguments)); var args = ["add"].concat(_.toarray(arguments));

javascript node.js

No comments:

Post a Comment