When I set arugments[0] = chewie; arguments[1] = luke
it prints:
Chewbacca
Chewbacca
Han Solo
Chewbacca
And what I expected was the following:
Chewbacca
Luke Skywalker
Han Solo
Chewbacca
function starWars(luke, darth, han, chewie) {
arguments[0] = chewie;
arguments[1] = luke;
document.write(arguments[0] + "<br>");
document.write(arguments[1] + "<br>");
document.write(arguments[2] + "<br>");
document.write(arguments[3] + "<br>");
}
starWars("Luke Skywalker", "Darth Vader", "Han Solo", "Chewbacca");
Can anyone explain me in details why it is not working as I expected?
arguments
manually. In almost every case, it's no longer necessary to use thearguments
object in ES2015 or laterarguments[0]
andluke
are the exact same thing. Not two references to the same value, but basically both are kind of the same variable. If you changearguments[0]
,luke
will also be changed; and vice versa. So much for the knowledge; no reason to explit or even use this. Listen to @naomik, avoid thearguments
object as much as possible.