javascript将对象传递给函数

del*_*006 3 javascript function object

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
Run Code Online (Sandbox Code Playgroud)

将对象o传递给函数时会发生什么?第一个功能使得它似乎没有通过; 第二个函数使它看起来像是通过了

Que*_*tin 5

第1行:创建一个对象.对它的引用传递给o.

第2行:调用一个函数.对象的引用作为参数传递.x然后用null(不接触对象本身或仍然分配给它的引用)覆盖引用(in o).

第3行:调用一个函数.对象的引用作为参数传递.A foobar属性将添加到对象和赋值.