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

RHH*_*RHH 1 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();
        };
}
Run Code Online (Sandbox Code Playgroud)

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

Poi*_*nty 8

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

var s = new slowbie();
s.tick();
Run Code Online (Sandbox Code Playgroud)

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

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

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

slowbie.tick();
Run Code Online (Sandbox Code Playgroud)

好吧,这将无法工作,因为名称"slowbie"指的是函数,并且该函数对象没有名为"tick"的属性.要获得"tick",您必须使用"slowbie"函数将对象实例化为构造函数:

var s = new slowbie();
Run Code Online (Sandbox Code Playgroud)

或者明确地使用"call"或者其他东西:

var s = slowbie.call(someObject);
someObject.tick();
Run Code Online (Sandbox Code Playgroud)

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

slowbie.call(slowbie);
Run Code Online (Sandbox Code Playgroud)

这将为"slowbie"对象本身添加一个"tick"属性(也就是"slowbie"实际上是的Function实例).此后,调用"slowbie.tick()"将起作用.