Javascript:同一命名空间内数组中的引用变量
我有以下名称空间Javascript:同一命名空间内数组中的引用变量,javascript,arrays,variables,namespaces,Javascript,Arrays,Variables,Namespaces,我有以下名称空间 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 loun
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']
]}
但当我控制台记录我的数据列表时,数据列表[0]的结果是:
0:NaN
1:NaN
2:NaN
3:NaN
4:droom.jpg
5:1号卧室
显然,数组中的'this.w'并不是指同一名称空间中的w:200,我做错了什么?任何建议都将不胜感激
谢谢此.w尚未成为属性,您需要两个步骤:
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']
);
此.w尚未成为属性,您需要两个步骤:
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']
);
您的代码在全局上下文中执行。这意味着它将引用窗口对象。如果希望此引用应用程序对象,则需要在其自己的方法中执行代码 变量应用={ w:200, h:200, 间距:5, 数据列表:函数{ 返回[ //第一列 [1*this.w,1*this.h,0*this.w,0*this.h,'doomy.jpg','doomy 1'], [1*this.w,1*this.h,0*this.w,1*this.h,'topFloorLounge.jpg','topFloorLounge'], [1*this.w,1*this.h,0*this.w,2*this.h,'garage.jpg','garage'], //第二栏 [2*this.w,2*this.h,1*this.w,0*this.h,'livingRoom2.jpg','客厅2'], [1*this.w,1*this.h,1*this.w,2*this.h,'gym.jpg','gym'] ]; } };
app.dataList 您的代码在全局上下文中执行。这意味着它将引用窗口对象。如果希望此引用应用程序对象,则需要在其自己的方法中执行代码 变量应用={ w:200, h:200, 间距:5, 数据列表:函数{ 返回[ //第一列 [1*this.w,1*this.h,0*this.w,0*this.h,'doomy.jpg','doomy 1'], [1*this.w,1*this.h,0*this.w,1*this.h,'topFloorLounge.jpg','topFloorLounge'], [1*this.w,1*this.h,0*this.w,2*this.h,'garage.jpg','garage'], //第二栏 [2*this.w,2*this.h,1*this.w,0*this.h,'livingRoom2.jpg','客厅2'], [1*this.w,1*this.h,1*this.w,2*this.h,'gym.jpg','gym'] ]; } };
app.dataList;谢谢你,你的答案更符合我的需要,因为它更独立,更接近我最初的意图。谢谢你,你的答案更符合我的需要,因为它更独立,更接近我最初的意图。