当变量引用对象时,值是对对象的引用(引用自:Javascript by reference vs. by value)
function fun1(obj) {
obj.first = 1; //it affects the object ->obj
}
var myObj1 = {first: 0};
fun1(myObj1); //sending the object to the function
console.log(myObj1.first); // 1
但我想改变一个对象的变量,例如
function fun2(obj) {
obj = 1; }
var myObj2 = {first: 0};
fun2(myObj2.first);
console.log(myObj2.first);
有什么办法可以做到这一点?