Arguments to actual array

Arguments to actual array
0.0 0


I saw on the documentation that slice method is used to convert Array-like objects (e.g. arguments) to a new array by using but I’m just curious if it is fine if we can use apply method. Like Array.prototype.slice.apply(arguments). Since according to the documentation that

the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments.

And since arguments is not actually an array. I mean which of the two is more conventional?


I don’t think if there is any differences! but it’s good both are there. Sometimes you have all variables you want to create an object, so may go for call, otherwise for apply.

About the call, I don’t know if it’s possible to send any value as argument to it. For instance in objc, for creating a NSArray, you may not ask to save nil as a null value. Because nil is defined as end of arguments, and the rest will be ignored.

About the js I think it does some internal changing for that, not sure.

I suggest you use constructor of the class, rather going more low level like this.


This is why there are the two function methods: sometimes what you need to pass in will be an array, sometimes individual arguments.

But there is a much easier way, which is Array.from, which creates an array from something like an array - Array.from(arguments) for example. call and apply have performance implications, JS engines can’t use their compiler step on Array.prototype.slice.apply(arguments) or any code it touches, they have to interpret it at runtime. Array.from avoids that issue. If you need to get an array of the arguments (which isn’t actually that common), then either that, or passing the unknown number of other arguments using rest parameters (function myFunction(arg1, arg2, ...someOtherArgs)), or destructuring arguments like [...arguments] are all better options.