js对象引用传递


今天写接口测试demo,发现js值引用问题

js 普通变量为值传递
js 对象为为引用传递
var a = 123;
undefined
var b=a;
undefined
a
123
b
123
b =234
234
a
123
a = 456
456
b
234
var o = {name:'lxb',age:21}
undefined
h = o
Object { name: "lxb", age: 21 }
h.width = 20
20
o
Object { name: "lxb", age: 21, width: 20 }

解决方案

function Dog(name, breed, color, sex) {
   this.name = name;
   this.breed = breed;
   this.color = color;
   this.sex = sex;
}

theDog = new Dog("Gabby", "Lab", "chocolate", "girl");

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toSource


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM