一個(gè)例子:
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);
結(jié)果如下:
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.