I’ve just realized about a creepy fact about JavaScript. Suppose you have this code:
function f(a, b, c) { arguments[1] = 99; return [a, b, c]; } f(1, 2, 3);
What do you think the result is?
[1, 2, 3]
You guess but NO. Right answer is:
[1, 99, 3]
Unless you’re in strict mode (and you should always be in strict mode), then result is [1, 2, 3]
.
In non strict mode, setting the n-th item of arguments
alters the value of the n-th formal parameter in the function signature. A really evil and unexpected side effect but specified in http://ecma-international.org/ecma-262/5.1/#sec-10.6 (see Note 1).
So be careful and keep yourself in strict mode where things are less unpredictable!
function f(a, b, c) { 'use strict'; arguments[1] = 99; return [a, b, c]; } f(1, 2, 3);