物件傳遞問題
var main = {
data : {},
setData : function() {
setObj(this.data)
alert(this.data.id);
}
};
function setObj(obj) {
var newData = {};
newData.id = "123";
obj = newData; // obj = main.data
}
The main.setData() alert is undefined.
Change setObj function :
function setObj(obj) {
var newData = {};
newData.id = "123";
obj.prop = newData;
}
The main.setData() alert(this.data.prop.id) is "123".
How to modify original setObj function
and main.setData() alert(this.data.id) is "123"?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.211.51.126
討論串 (同標題文章)
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章