bedward
November 27, 2020, 10:50am
1
Please consider the line of code below:
var args = Array.prototype.slice.call(arguments);
isn’t args = Array.prototype.slice.call(arguments)
like doing : args = arguments.slice()
, if not, why and what is the difference please explain if possible thanks <3
sitek94
November 27, 2020, 10:58am
2
Have you tried doing this? What happened?
args = arguments.slice()
bedward
November 27, 2020, 11:12am
3
TypeError: arguments.slice is not a function
sitek94
November 27, 2020, 11:13am
4
Why do you think slice
is not a function?
bedward
November 27, 2020, 11:15am
5
It was the console message not me
the console returns that message
sitek94
November 27, 2020, 11:18am
6
Yeah I know and it’s true - it’s not a function
I’m asking you, why it’s not a function?
You can always check. If it is not a function, then what it is?
console.log(arguments.slice)
bedward
November 27, 2020, 11:25am
7
i don’t know, can you tell me?
sitek94
November 27, 2020, 11:26am
8
So, what did you get when you console.log arguments.slice
?
bedward
November 27, 2020, 11:30am
9
Do you know the arguments
object?
sitek94
November 27, 2020, 11:36am
10
What do you mean do I know it?
I think you answered the question yourself with this link:
arguments
is an Array
-like object
You cannot use slice
method, because there is no slice
method. If you would console.log(arguments.slice) you would get undefined
because slice
is specific to arrays which arguments
is not.
That’s why we have to use Array.prototype
.
bedward
November 27, 2020, 11:37am
11
but why does it work with Array.prototype ?
sitek94
November 27, 2020, 11:41am
12
MDN :
The arguments
object is not an Array
. It is similar, but lacks all Array
properties except length
. For example, it does not have the pop()
method.
However, it can be converted to a real Array
:
var args = Array.prototype.slice.call(arguments);
1 Like
jsdisco
November 27, 2020, 1:52pm
13
A somewhat more readable version would be
var args = Array.from(arguments).slice()
arguments
is an Array-like object, which means that
the JavaScript Array methods can’t be used on it, but
it can be converted to an Array with Array.from()
1 Like