【发布时间】:2013-04-09 11:08:55
【问题描述】:
var o = {};
(function(x){x=null})(o); //o is NOT null after this statement
(function(x){x.foo = "foo"; x.bar = "bar";})(o) //o has properties foo and bar after this statement
将对象 o 传递给函数时发生了什么?第一个函数使它看起来没有通过;第二个函数看起来好像通过了
【问题讨论】:
-
在第一个函数中,您将 null 分配给变量
x,而不是o。如果你想将它分配给o,你必须通过引用传递它。
标签: javascript function object