Friday, November 1, 2013

Convert arguments into array in javascript

Let see this senario:

var func = function(){
      arguments.push('foo');
}

Then you run func('a', 'b', 'c'), you will get Object has no method 'push'.

To enable your arguments being treated as array,

var func = function(){
        var arry = Array.prototype.slice.call(arguments);
        arry.push('foo');
}

You would able to use the push method, because you have converted the arguments into array.

No comments:

Post a Comment