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