我有以下命名空间,
var app = {
w: 200,
h: 200,
spacing: 5,
dataList:[
// 1st column
[1 * this.w, 1 * this.h, 0 * this.w, 0 * this.h, 'bedroom.jpg', 'Bedroom 1'],
[1 * this.w, 1 * this.h, 0 * this.w, 1 * this.h, 'topFloorLounge.jpg', 'Top floor lounge'],
[1 * this.w, 1 * this.h, 0 * this.w, 2 * this.h, 'garage.jpg', 'Garage'],
// 2nd column
[2 * this.w, 2 * this.h, 1 * this.w, 0 * this.h, 'livingRoom2.jpg', 'Living room 2'],
[1 * this.w, 1 * this.h, 1 * this.w, 2 * this.h, 'gym.jpg', 'Gym']
]}
但是当我控制登录我的dataList时,dataList [0]的结果是:
0: NaN
1: NaN
2: NaN
3: NaN
4: "bedroom.jpg"
5: "Bedroom 1"
由于
答案 0 :(得分:3)
this.w还不是属性,需要2个步骤:
var app = {
w: 200,
h: 200,
spacing: 5,
dataList:[]
};
app.dataList.push(
// 1st column
[1 * app.w, 1 * app.h, 0 * app.w, 0 * app.h, 'bedroom.jpg', 'Bedroom 1'],
[1 * app.w, 1 * app.h, 0 * app.w, 1 * app.h, 'topFloorLounge.jpg', 'Top floor lounge'],
[1 * app.w, 1 * app.h, 0 * app.w, 2 * app.h, 'garage.jpg', 'Garage'],
// 2nd column
[2 * app.w, 2 * app.h, 1 * app.w, 0 * app.h, 'livingRoom2.jpg', 'Living room 2'],
[1 * app.w, 1 * app.h, 1 * app.w, 2 * app.h, 'gym.jpg', 'Gym']
);
答案 1 :(得分:1)
您的代码在全局上下文中执行。这意味着this
将引用window
对象。如果您希望this
引用app
对象,则需要在自己的方法中执行代码。
var app = {
w: 200,
h: 200,
spacing: 5,
dataList: function() {
return [
// 1st column
[1 * this.w, 1 * this.h, 0 * this.w, 0 * this.h, 'bedroom.jpg', 'Bedroom 1'],
[1 * this.w, 1 * this.h, 0 * this.w, 1 * this.h, 'topFloorLounge.jpg', 'Top floor lounge'],
[1 * this.w, 1 * this.h, 0 * this.w, 2 * this.h, 'garage.jpg', 'Garage'],
// 2nd column
[2 * this.w, 2 * this.h, 1 * this.w, 0 * this.h, 'livingRoom2.jpg', 'Living room 2'],
[1 * this.w, 1 * this.h, 1 * this.w, 2 * this.h, 'gym.jpg', 'Gym']
];
}
};
app.dataList();