Monday, May 27, 2013

What Array.prototype.slice.call(arguments) does in javascript

Why using Array.prototype.slice.call(arguments) in javascript? Basically it is used to convert multiple arguments into a single argument in array type, as the some js method may only accept one argument instead of several arguments.

for example:
func2(arg){
  console.log(arg);
}

func1(arg1, arg2, arg3)
{
   arg = Array.prototype.slice.call(arguments);
   func2(arg);
}

It can be understood as the following conversion:
    Array.prototype.slice.call(arguments);
==  Array.prototype.slice(arguments[1], arguments[2], arguments[3], ...)
==  [ arguments[1], arguments[2], arguments[3], ... ]

No comments:

Post a Comment