我在访问对象中的变量时遇到了一些问题。引用此内部对象
当我尝试访问的变量我就要返回undefined。
我的代码如下所示:
var app = {
data: function() {
this.labels = [1, 2, 3, 4, 5];
},
barChartData: {
labels: this.labels, // this is undefined
datasets: [{
data: this.data1 // this is undefined
}, {
data: this.data2 // this is undefined
}]
},
},
init: function() {
this.data();
}
}
app.init();
'barChartData'是在调用'data()'之前定义的,只需在** init **函数末尾添加'this.barChartData.labels = this.labels'即可。 'data1'和'data2'没有定义,所以它们的值当然是未定义的 –
[对象字面声明中的自引用]的可能重复(http://stackoverflow.com/questions/4616202/self-references-in-对象文字声明) – nils