我在訪問對象中的變量時遇到了一些問題。引用此內部對象
當我嘗試訪問的變量我就要返回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