作者gpmm (银色)
看板Ajax
标题Re: 物件传递问题
时间Mon Jan 14 17:59:43 2008
※ 引述《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