0

Why can't we directly call the functions from Array on arguments?

function f(){
   var x = Array.prototype.slice.call(arguments,1);
   //var x = arguments.slice(1);  **error**
 alert(x);
}

f(1,2,3);

Update: If arguments is not an array object, then how does the Array.prototype.slice function works on an non-array object?

1
  • That will simply copy the members of arguments to a new array as if by a simple for loop that iterates over it from 0 to length - 1. Early versions of Opera implemented arguments as an array, but that was not compliant with ECMA-262 so it stopped doing that. Commented Apr 25, 2014 at 9:46

1 Answer 1

4

Because, despite having some array-like qualities, the arguments object is not an Array.

It is defined independently of, and without reference to, Array objects.

Sign up to request clarification or add additional context in comments.

2 Comments

So how does the prototype.slice function works on an non-array?
The same way as it works on an array. It looks at the value of this and then manipulates it. The arguments object has all the array-like qualities needed by the function. It just doesn't have the function itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.