简体   繁体   English

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

[英]Uncaught TypeError: Object function has no method

I'm sure this is very readily solved. 我相信这很容易解决。 I'm trying to invoke slowbie.tick(); 我正在尝试调用slowbie.tick();

Here's the code: 这是代码:

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

I don't understand why tick apparently isn't a privileged method... Please assist! 我不明白为什么打勾显然不是特权方法……请协助!

You're explicitly exposing the "tick" function on the context object ( this ). 您将在上下文对象( this )上显式公开“ tick”功能。 If you do something like: 如果您执行以下操作:

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

then that works because your code explicitly arranged for it to work. 那么就可以了,因为您的代码明确安排了它的工作。

In JavaScript, functions are functions. 在JavaScript中,函数就是函数。 If you can get a reference to a function, you can always call it, no matter how it was defined. 如果可以获得对函数的引用,则无论如何定义,都可以随时调用它。 There's no such thing as "privileged" or "private" functions, at least not as far as the functions themselves are concerned. 没有所谓的“特权”或“私有”功能,至少就功能本身而言。 The real issue is visibiity . 真正的问题是可见性 If a function is declared locally inside another function, and nothing in the outer function ever exposes a reference to the inner function, then nothing outside the outer function can get at the inner function. 如果一个函数是在另一个函数内部局部声明的,而外部函数中没有任何东西公开对内部函数的引用,那么外部函数之外的任何东西都不会到达内部函数。 The inner function doesn't really "know" that, however, and if a reference does leak out then it can be called freely. 内部函数并不真正“知道”这一点,如果引用确实泄漏出去,则可以自由调用它。

Now, that's the answer to the question at the end of your post. 现在,这就是帖子结尾处问题的答案。 As to the title of your post, well, it's not entirely clear what you're up to. 至于您的帖子标题,还不清楚您要做什么。 If you try this: 如果您尝试这样做:

slowbie.tick();

well that won't work because the name "slowbie" refers to the function, and that function object has no property called "tick". 好吧,因为名称“ slowbie”是指该函数,并且该函数对象没有称为“ tick”的属性,因此不会起作用。 To get at "tick", you have to instantiate an object with the "slowbie" function as a constructor: 要获得“ tick”,必须使用“ slowbie”功能将其实例化为构造函数:

var s = new slowbie();

or else explicitly use "call" or something: 或者明确使用“通话”或其他方式:

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

Finally note that if you really want to be able to call "slowbie.tick()", then you could always do this: 最后请注意,如果您确实希望能够调用“ slowbie.tick()”,则可以始终这样做:

slowbie.call(slowbie);

That will add a "tick" property to the "slowbie" object itself (that is, the Function instance that "slowbie" actually is). 这会将“ tick”属性添加到“ slowbie”对象本身(即“ slowbie”实际上是Function实例)。 Thereafter, calls to "slowbie.tick()" will work. 此后,将调用“ slowbie.tick()”。

What makes you think it isn't? 是什么让您认为不是? How are you trying to call tick() ? 您如何尝试调用tick()

You may want to look over Crockford's page discussing this (har har) because there are a few other issues with what I think you're trying to do, but can't quite tell. 您可能希望查看Crockford的讨论此页面的内容,因为我认为您要尝试执行的操作还有其他一些问题,但还不能完全确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method 未捕获的TypeError:对象函数()没有方法项 - Uncaught TypeError: Object function () has no method items 未捕获的TypeError:对象函数()没有方法“替换” - Uncaught TypeError: Object function () has no method 'replace' 未捕获的TypeError:对象[object Object]没有方法&#39;tagsInput&#39;(匿名函数) - Uncaught TypeError: Object [object Object] has no method 'tagsInput' (anonymous function) 未捕获的TypeError:对象函数(a){返回新j(a)}没有方法&#39;has&#39; - Uncaught TypeError: Object function (a){return new j(a)} has no method 'has' 未捕获的TypeError:对象没有方法“打开” - Uncaught TypeError: Object has no method 'on' 未捕获的TypeError:对象[object Window]没有方法&#39;each&#39;函数 - Uncaught TypeError: Object [object Window] has no method 'each' function 未捕获的typeerror:对象# <object> 没有方法“方法” - Uncaught typeerror: Object #<object> has no method 'method' 未捕获的TypeError:对象(JS函数)没有方法“应用” - Uncaught TypeError: Object (JS Function) has no method 'apply' Require.js:未捕获的TypeError:对象函数…没有方法 - Require.js : Uncaught TypeError: Object function … has no method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM