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,  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

原文地址:https://www.cnblogs.com/lixingbaophp/p/5613777.html