美文网首页
Javascript 语言是值传递还是引用传递?

Javascript 语言是值传递还是引用传递?

作者: zhuang_niu | 来源:发表于2017-02-27 16:29 被阅读0次

一个例子:

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.

相关文章

网友评论

      本文标题:Javascript 语言是值传递还是引用传递?

      本文链接:https://www.haomeiwen.com/subject/sohtwttx.html