未捕获的ReferenceError:未定义细节

时间:2022-02-11 19:49:36

When I run the code below in chrome,

当我用chrome运行下面的代码时,

I got error "Uncaught ReferenceError: detail is not defined";

我得到错误"Uncaught ReferenceError: detail未定义"

I try to define object shape with the name hamburgers and then to create the hamburger.

我尝试用汉堡包的名字定义对象形状,然后创建汉堡包。

what is wrong in the code

代码有什么问题

Many thanks.

多谢。

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);
    }

2 个解决方案

#1


0  

detail isn't defined because you are actually trying to find shape.details[0]. Defining detail as that array item fixes it.

细节没有定义,因为您实际上正在尝试寻找shape.details[0]。在数组项中定义细节可以修复它。

http://jsfiddle.net/EaPr7/

http://jsfiddle.net/EaPr7/

#2


0  

detail is indeed not defined. Try this:

细节确实没有定义。试试这个:

detail = shape.details[0];

i = 0;
shape.hamburgers.push(new Hamburger(detail.hamburgers[i].x, ...

#1


0  

detail isn't defined because you are actually trying to find shape.details[0]. Defining detail as that array item fixes it.

细节没有定义,因为您实际上正在尝试寻找shape.details[0]。在数组项中定义细节可以修复它。

http://jsfiddle.net/EaPr7/

http://jsfiddle.net/EaPr7/

#2


0  

detail is indeed not defined. Try this:

细节确实没有定义。试试这个:

detail = shape.details[0];

i = 0;
shape.hamburgers.push(new Hamburger(detail.hamburgers[i].x, ...