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 传递给函数时发生了什么?第一个函数使它看起来没有通过;第二个功能使 o 似乎通过了
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 传递给函数时发生了什么?第一个函数使它看起来没有通过;第二个功能使 o 似乎通过了
第 1 行:创建了一个对象。对它的引用被传递给o.
第 2 行:调用一个函数。对对象的引用作为参数传递。然后引用 (in x) 被覆盖null(不触及对象本身或仍然分配给它的引用o)。
第 3 行:调用一个函数。对对象的引用作为参数传递。将 Afoo和bar属性添加到对象并分配值。
(function(x){
x=null
})(o);
x“指向” o,但您只是指定x指向另一事物,null因此不会改变o。
(function(x){
x.foo = "foo";
x.bar = "bar";
})(o);
在此代码中,您正在更改指向的任何内容的属性(添加foo和),即. 这种改动将反映到.barxoo