javascript 未捕获的类型错误:对象函数没有方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7423773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Object function has no method
提问by RHH
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!
我不明白为什么滴答显然不是一种特权方法......请帮忙!
回答by Pointy
You're explicitly exposing the "tick" function on the context object (this
). If you do something like:
您明确地公开了上下文对象 ( this
)上的“tick”函数。如果您执行以下操作:
var s = new slowbie();
s.tick();
then that works because your code explicitly arranged for it to work.
那么这是有效的,因为您的代码明确安排了它的工作。
In JavaScript, functions are functions. If you can get a reference to a function, you can alwayscall 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.
在 JavaScript 中,函数就是函数。如果你能得到一个函数的引用,你总是可以调用它,不管它是如何定义的。没有“特权”或“私有”功能之类的东西,至少就功能本身而言没有。真正的问题是可见性。如果一个函数在另一个函数内部局部声明,并且外部函数中没有任何内容公开对内部函数的引用,那么外部函数之外的任何内容都无法在内部函数中获得。然而,内部函数并不真正“知道”这一点,如果引用确实泄漏了,则可以自由调用它。
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". To get at "tick", you have to instantiate an object with the "slowbie" function as a constructor:
好吧,这行不通,因为名称“slowbie”指的是函数,而该函数对象没有名为“tick”的属性。要获得“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 reallywant 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). Thereafter, calls to "slowbie.tick()" will work.
这将为“slowbie”对象本身添加一个“tick”属性(即“slowbie”实际上是的函数实例)。此后,对“slowbie.tick()”的调用将起作用。
回答by Dave Newton
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 thinkyou're trying to do, but can't quite tell.
您可能想查看Crockford 讨论此问题的页面(har har),因为我认为您正在尝试做的事情还有其他一些问题,但不能完全说明。