我试图在另一个对象的回调方法中设置一个对象(摘要)
returnObj.beforeLoadComplete = function (records) { var someObj = { total: { label: 'Items', value: '15' }, additional: [{ label: 'Item1 Total', value: '25000' }] }; returnObj.summary = summaryObj; // some other code which finally returns an object }
上面的代码不起作用(即summary
未设置returnObj
)
但是,如果我在回调方法之外有相同的代码,它就像在下面的代码片段中一样:
var someObj = { total: { label: 'Items', value: '15' }, additional: [{ label: 'Item1 Total', value: '25000' }] }; returnObj.summary = summaryObj; returnObj.beforeLoadComplete = function (records) { // some other code which finally returns an object }
不知道为什么会这样.
你必须用这个语句访问你的对象,我也纠正了一些错字:
var returnObj = {}; returnObj.beforeLoadComplete = function (records) { var someObj = { total: { label: 'Items', value: '15' }, additional: [{ label: 'Item1 Total', value: '25000' }] }; // Access object with this this.summary = someObj; // some other code which finally returns an object } returnObj.beforeLoadComplete('records'); console.log(returnObj.summary);
更新:添加了代码段以验证是否returnObj
可以通过回调处理程序中的此访问.
var returnObj = {};
returnObj.beforeLoadComplete = function () {
var someObj = {
total: {
label: "Items",
value: "15"
},
additional: [{
label: 'Item1 Total',
value: '25000'
}]
};
this.summary = someObj;
// some other code which finally returns an object
}
//returnObj.beforeLoadComplete();
function verifyObjectUpdated(){
alert(returnObj.summary);
}