未捕获的TypeError:对象函数没有方法

时间:2011-09-14 22:29:49

标签: javascript typeerror

我确信这很容易解决。我正在尝试调用slowbie.tick();

以下是代码:

 function slowbie(){ //Enemy that slowly moves towards you.
    this.max = 7;
    this.w = 25;
    this.h = 25;
    this.speed = 5;
    this.points = 1;
    this.enemies = [];
    function spawn(){
        if(this.enemies < this.max){
            for (var i = this.enemies.length; i < this.max; i++) {
            this.x = width + Math.floor(Math.random()*20) + this.w;
            this.y = Math.floor(Math.random()*(height-this.h));
            this.speed = Math.floor(Math.random()*(speed-1))+1;
            this.enemies.push(
                [this.x, this.y, this.w, this.h, this.speed]);
            }
        }
    }

    function move(){
        for (var i = 0; i < this.enemies.length; i++) {
            if (this.enemies[i][0] > -this.w) {
            this.enemies[i][0] -= this.enemies[i][4];
        } else{
            this.enemies[i][0] = width;
            this.enemies[i][1] = Math.floor(Math.random()*(height-this.h));
            this.enemies[i][4] = Math.floor(Math.random()*(this.speed-1))+1;
            }
        }
    }

    function hit(){
        var remove = false;
        for (var i = 0; i < lasers.length; i++) {
            for (var j = 0; j < this.enemies.length; j++){
                if (lasers[i][0] <= (this.enemies[j][0] + this.enemies[j][2]) &&
                    lasers[i][0] >= this.enemies[j][0] &&
                    lasers[i][1] >= this.enemies[j][1] &&
                    lasers[i][1] <= (this.enemies[j][1] + this.enemies[j][3])) {
                remove = true;
                this.enemies.splice(j, 1);
                score += this.points;
                spawn();
            }
            }
            if (remove) {
            lasers.splice(i, 1);
            remove = false;
            }
        }
    }

    function draw(){
        for (var i = 0; i < this.enemies.length; i++) {
            ctx.fillStyle = '#f00';
            ctx.fillRect(this.enemies[i][0], this.enemies[i][1], this.w, this.h);
            }
        }

    this.tick = function(){
        spawn();
        hit();
        draw();
        move();
        };
}

我不明白为什么蜱显然不是特权方法......请协助!

2 个答案:

答案 0 :(得分:8)

您明确地在上下文对象(this)上公开了“tick”函数。如果您执行以下操作:

var s = new slowbie();
s.tick();

然后这是有效的,因为你的代码明确地安排它工作。

在JavaScript中,函数是函数。如果您可以获得对函数的引用,则无论如何定义,您都可以始终调用它。没有“特权”或“私人”功能,至少就功能本身而言并非如此。真正的问题是 visibiity 。如果函数在另一个函数内部声明,并且外部函数中没有任何内容暴露出对内部函数的引用,那么外部函数之外的任何内容都不能获取内部函数。然而,内部函数并不真正“知道”,如果引用漏出,则可以自由调用它。

现在,这是你帖子结尾处问题的答案。至于你的帖子的标题,嗯,你不清楚你在做什么。如果你试试这个:

slowbie.tick();

好吧,这是行不通的,因为名称“slowbie”指的是函数,而且该函数对象没有名为“tick”的属性。要获得“tick”,你必须使用“slowbie”函数作为构造函数实例化一个对象:

var s = new slowbie();

或明确使用“call”或其他内容:

var s = slowbie.call(someObject);
someObject.tick();

最后请注意,如果您真的希望能够调用“slowbie.tick()”,那么您可以随时执行此操作:

slowbie.call(slowbie);

这将为“slowbie”对象本身添加“tick”属性(即“slowbie”实际上是的Function实例)。此后,对“slowbie.tick()”的调用将起作用。

答案 1 :(得分:0)

是什么让你认为不是?您是如何致电tick()的?

你可能想要查看Crockford's page discussing this(har har),因为我正在尝试做的事情还有一些其他问题,但是无法说清楚。