Re: 物件傳遞問題
※ 引述《tirme (Tirme)》之銘言:
: 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"?
the first setObj is doing `pass by value`, not `pass by reference`.
js does not support passing by reference for types such as string,
number, or boolean.
however, it does automatically pass by reference objects, functions,
arrays, HTMLElements and other complex objects
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.19.91
討論串 (同標題文章)
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章