Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当您使用JavaScript函数作为类时,它们是否仍然以自顶向下的方式进行解析?_Javascript_Jquery - Fatal编程技术网

当您使用JavaScript函数作为类时,它们是否仍然以自顶向下的方式进行解析?

当您使用JavaScript函数作为类时,它们是否仍然以自顶向下的方式进行解析?,javascript,jquery,Javascript,Jquery,我试图弄明白为什么我的谷歌浏览器控制台会给我一个错误:“未定义不是一个函数。”我有预感,但也许我走错了方向。我的函数boxCollision(…)在类的底部定义。接近顶端时,我有一个声明 if (this.boxCollision(this.food.getBBox(), this.body[0].getBBox())) this.food.translate(this.linkSize, 0); 其中的第一行导致了我提到的错误。我想这可能是因为我还没有定义bo

我试图弄明白为什么我的谷歌浏览器控制台会给我一个错误:“未定义不是一个函数。”我有预感,但也许我走错了方向。我的函数
boxCollision(…)
在类的底部定义。接近顶端时,我有一个声明

    if (this.boxCollision(this.food.getBBox(), this.body[0].getBBox()))
            this.food.translate(this.linkSize, 0);
其中的第一行导致了我提到的错误。我想这可能是因为我还没有定义boxCollision,所以它根本不存在。是这样吗?可以识别
getBBox()
函数,因为它们来自外部JavaScript文件

    function snakegame(C, C_w, C_h, spd)
    {
            /* NOTE TO SELF: C is a Raphel object. Can't find a method to return the height
               and width of a Raphael object in the documentation: 
               http://raphaeljs.com/reference.html#Raphael.
               Using C_h and C_w for now, but should probably change it later.
            */


            this.linkSize = 50; /* size of a snake unit, in pixels; must divide C_h and C_w */


            this.link = C.rect(C_h/2, C_w/2, this.linkSize, this.linkSize);
            this.link.attr("fill", "#E9E581");
            this.body = [this.link];

            this.food = C.rect(randInt(0,C_w/this.linkSize-1) * this.linkSize, randInt(0,C_h/this.linkSize-1) * this.linkSize, this.linkSize, this.linkSize);
            if (this.boxCollision(this.food.getBBox(), this.body[0].getBBox()))
                this.food.translate(this.linkSize, 0);
            this.food.attr("fill","#B43535");

            this.maxSnakeSize = C_h * C_w / (this.linkSize * this.linkSize);

            /* On instantiation, the snake direction is down and has 1 link */
            this.dy = 0;
            this.dx = 0;


            this.score = 0;

            /* Event listener for changing the direction of the
               snake with arroy keys on the keyboard
            */
            this.redirect = function(dirnum)
            {
                switch (dirnum)
                {
                    /*
                        dirnum corresponds to
                        1 ---> right
                        2 ---> down
                        3 ---> left
                        4 ---> up
                    */
                    case 1: 
                        this.dx = this.linkSize;
                        this.dy = 0;
                        break;

                    case 2:
                        this.dx = 0;
                        this.dy = this.linkSize;
                        break;

                    case 3:
                        this.dx = -this.linkSize;
                        this.dy = 0;
                        break;

                    case 4:
                        this.dx = 0;
                        this.dy = -this.linkSize;
                        break;

                    default: /* never happens */
                        break;
                }

            }
            this.move = function()
            {

                if (this.body.length == this.maxSnakeSize)
                {
                    this.destruct();
                    return;
                }

                var addLink = false;
                var BBhead = this.body[0].getBBox();
                if (this.hitWall(BBhead) || this.hitSnake(BBhead))
                {
                    document.getElementById("snakescorediv").innerHTML = "<p>GAME OVER!</p><p>Score: "+ this.score +"</p>";
                    this.destruct();
                    return;
                }
                var BBfood = this.food.getBBox();
                if (this.boxCollision(BBhead, BBfood))
                {
                    this.moveFood();
                    this.score += 10;
                    document.getElementById("snakescorediv").innerHTML = this.score.toString();
                    addLink = true;
                }
                if (addLink)
                    this.body.push(this.body[this.body.length - 1].clone());
                for (var i = this.body.length - 1; i > 0; --i)
                {
                    var prevBB = this.body[i-1].getBBox();
                    var thisBB = this.body[i].getBBox();
                    this.body[i].translate(prevBB.x-thisBB.x, prevBB.y-thisBB.y)
                }
                this.body[0].translate(this.dx, this.dy);

            }

            this.mover = setInterval(this.move.bind(this), spd);   

            this.hitWall = function(bb)
            {
                return bb.x < 0 || bb.x2 > C_w || bb.y < 0 || bb.y2 > C_h;
            }

            this.hitSnake = function(bb)
            {
                var retval = false;
                for (var i = 1, j = this.body.length; i < j; ++i)
                {
                    var thisbb = this.body[i].getBBox();
                    if (this.boxCollision(bb, thisbb))
                    {
                        retval = true;
                        break;
                    }
                }
                return retval;
            }

            this.moveFood = function()
            {   
                var bbf = this.food.getBBox(); // bounding box for food
                do {
                /* tx, ty: random translation units */
                tx = randInt(0, C_w / this.linkSize - 1) * this.linkSize - bbf.x;
                ty = randInt(0, C_h / this.linkSize - 1) * this.linkSize - bbf.y;
                 // translate copy of food
                this.food.translate(tx, ty);
                bbf = this.food.getBBox(); // update bbf
                } while (this.hitSnake(bbf));

            }

            this.boxCollision = function(A, B)
            {
                return A.x == B.x && A.y == B.y;
            }


            this.destruct = function()
            {
                clearInterval(this.mover); 
                for (var i = 0, j = this.body.length; i < j; ++i)
                {
                    this.body[i].removeData();
                    this.body[i].remove();
                }
                this.food.removeData();
                this.food.remove();
                this.score = 0;
            }

    }
功能蛇类游戏(C、C、w、C、h、spd)
{
/*自我提示:C是Raphel对象。找不到返回高度的方法
文档中拉斐尔对象的宽度:
http://raphaeljs.com/reference.html#Raphael.
现在使用C_h和C_w,但以后可能会更改。
*/
this.linkSize=50;/*蛇形单位的大小,以像素为单位;必须除以C_h和C_w*/
this.link=C.rect(C_h/2,C_w/2,this.linkSize,this.linkSize);
this.link.attr(“fill”和“#E9E581”);
this.body=[this.link];
this.food=C.rect(randInt(0,C_w/this.linkSize-1)*this.linkSize,randInt(0,C_h/this.linkSize-1)*this.linkSize,this.linkSize,this.linkSize);
if(this.boxCollision(this.food.getBBox(),this.body[0].getBBox()))
this.food.translate(this.linkSize,0);
这个.food.attr(“fill”,“#B43535”);
this.maxSnakeSize=C_h*C_w/(this.linkSize*this.linkSize);
/*在实例化时,蛇的方向是向下的,并且有一个链接*/
这是0.dy=0;
该值为0.dx=0;
这个分数=0;
/*事件侦听器,用于更改事件的方向
键盘上有arroy键的蛇
*/
this.redirect=函数(dirnum)
{
开关(dirnum)
{
/*
dirnum对应于
1--->对
2--->向下
3--->左
4--->向上
*/
案例1:
this.dx=this.linkSize;
这是0.dy=0;
打破
案例2:
该值为0.dx=0;
this.dy=this.linkSize;
打破
案例3:
this.dx=-this.linkSize;
这是0.dy=0;
打破
案例4:
该值为0.dx=0;
this.dy=-this.linkSize;
打破
默认值:/*从不发生*/
打破
}
}
this.move=函数()
{
if(this.body.length==this.maxSnakeSize)
{
这是destruct();
返回;
}
var addLink=false;
var BBhead=this.body[0].getBBox();
如果(这个.hitWall(BBhead)|这个.hitSnake(BBhead))
{
document.getElementById(“snakescorediv”).innerHTML=“游戏结束!

分数:“+this.Score+”

”; 这是destruct(); 返回; } var BBfood=this.food.getBBox(); 如果(这个盒子碰撞(BBhead,BBfood)) { 这是我的食物; 这个分数+=10; document.getElementById(“SnakeCoreDiv”).innerHTML=this.score.toString(); addLink=true; } 如果(添加链接) this.body.push(this.body[this.body.length-1].clone()); for(var i=this.body.length-1;i>0;--i) { var prevBB=this.body[i-1].getBBox(); var thisBB=this.body[i].getBBox(); this.body[i].translate(prevBB.x-thisBB.x,prevBB.y-thisBB.y) } this.body[0].translate(this.dx,this.dy); } this.mover=setInterval(this.move.bind(this),spd); this.hitWall=函数(bb) { 返回bb.x<0 | | bb.x2>C|w | | bb.y<0 | | bb.y2>C|h; } this.hitSnake=函数(bb) { var-retval=false; 对于(变量i=1,j=this.body.length;ifunction Ctor() { this.init() this.init = function() { console.log('init') } } var inst = new Ctor // Error: undefined is not a function
function Ctor() {
  this.init()
}

Ctor.prototype.init = function() {
  console.log('init')
}

var inst = new Ctor // init
function a () {}
var x
var x = 10;
    ^    ^
    |    |______ this part now gets assigned to `x` in the evaluation phase
    |
 this part was processed in the compilation phase
var x = function () {}
        function boxCollision (A, B) {
            return A.x == B.x && A.y == B.y;
        }
        this.boxCollision = boxCollision;
if (boxCollision(this.food.getBBox(), this.body[0].getBBox()))
        this.food.translate(this.linkSize, 0);