Tuesday, December 7, 2010

javascript的arguments Object


http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/
提到了 arguments

特別的是
他在foo(x,y,z)裡提到了一個例子 並且還在例子後註明了這是個chrome的bug


例子是:
如果改了arguments[2] 那z 同時也會改變嗎?
一般的javascript variable是不會的
但這裡是個特別的例子 blog的作者可能有點誤會了
在這裡  arguments[2]跟z是shared同個reference的


但ECMA  spec裡講到

10.1.8 Arguments Object
...........略
• For each non-negative integer, arg, less than the value of the length property, a property is created with name
ToString(arg) and property attributes { DontEnum }. The initial value of this property is the value of the
corresponding actual parameter supplied by the caller. The first actual parameter value corresponds to arg = 0,
the second to arg = 1, and so on. In the case when arg is less than the number of formal parameters for the
Function object, this property shares its value with the corresponding property of the activation object. This
means that changing this property changes the corresponding property of the activation object and vice versa.

在Javascript: The Definitive Guide也有提到這件事





Section 8.2.2
page 130

No comments: