Javascript 语言是值传递还是引用传递?
2017-02-27 本文已影响0人
zhuang_niu
一个例子:
function changeStuff(a,b,c){
a=a*10;
b.item="changed";
c={item:"changed"};
}
varnum=10;
varobj1={item:"unchanged"};
varobj2{item:"unchanged"};
changeStuff(num,obj1,obj2);
console.log(num);
console.log(obj1.item);
console.log(obj2.item);
结果如下:
10
changed
unchanged
Javascript 是纯粹的值传递.
It's always pass by value, but for objects the value of the variable is a reference. Because of this, when you pass an object and change itsmembers, those changes persist outside of the function. This makes itlooklike pass by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it's really pass by value.