当我在chrome中运行以下代码时,
我收到错误“Uncaught ReferenceError:detail is not defined”;
我尝试用名称hamburgers定义对象形状,然后创建汉堡包。
代码中出现了什么问题
非常感谢。
function Hamburger(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
var shape = {
hamburgers: [],
};
function drawshape(x, y, w, h) {
alert(x+' '+y+' '+w+' '+h);
}
shape.details =
[
{
"detail" : 0,
"hamburgers" : [{"x" : -290, "y" : -140,"w" : -290, "h" : -140}]
}
];
i = 0;
shape.hamburgers.push(new Hamburger(detail.hamburgers[i].x, detail.hamburgers[i].y,detail.hamburgers[i].w,detail.hamburgers[i].h));
for(var i=0;i<shape.hamburgers.length;i++) {
var hamburger = shape.hamburgers[i];
var x = hamburger.x;
var y = hamburger.y;
var w = hamburger.w;
var h = hamburger.h;
drawshape(x, y, w, h);
}
答案 0 :(得分:0)
detail
确实没有定义。试试这个:
detail = shape.details[0];
i = 0;
shape.hamburgers.push(new Hamburger(detail.hamburgers[i].x, ...
答案 1 :(得分:0)
detail
未定义,因为您实际上是在尝试查找shape.details[0]
。在该数组项修复它时定义detail
。