Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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 为什么object看不到他的方法?_Javascript_Html5 Canvas - Fatal编程技术网

Javascript 为什么object看不到他的方法?

Javascript 为什么object看不到他的方法?,javascript,html5-canvas,Javascript,Html5 Canvas,我正在使用atomJS框架和libCanvas库开发一个游戏。以下是发生错误的代码: var Planet=atom.Class({ //other code clearLayer : function (layer) { layer.ctx.clearRect(this.x, this.y, this.size, this.size); }, colonize : function (layer, angle, color,ms) { **this.cl

我正在使用atomJS框架和libCanvas库开发一个游戏。以下是发生错误的代码:

var Planet=atom.Class({
//other code
clearLayer : function (layer) {
        layer.ctx.clearRect(this.x, this.y, this.size, this.size);
    },
colonize : function (layer, angle, color,ms) {
        **this.clearLayer(layer);**
        drawArc({
            context: layer.ctx,
            x: Math.round(this.x + this.size / 2),
            y: Math.round(this.y + this.size / 2),
            radius: this.radius + 5,
            width: 4,
            color: color,
            opacity: 0.6,
            angleFinish: angle
        });
        if (this.colonizing) {
            //if (this.cursorOnPlanet()) this.context.fillText(COLONIZING, (this.x + this.size / 2) - 30, this.y + this.size - 2);
            this.colonizingTimer = setTimeout(this.colonize, ms,layer, angle + 5, color,ms);
            if (angle > 360) {
                this.colonizing = false;
                this.state = 1;
            }
        } else {
            clearTimeout(this.colonizingTimer);
            this.clearLayer(layer);
        }
    },
});
在这一行,
this.clearLayer(层)脚本终止时出现错误
对象[Object DOMWindow]没有方法“clearLayer”
。请告诉我问题出在哪里?
谢谢

了解
什么是verobject.colonize()
实际上是如何被调用的,这一点很重要。无论如何,很明显,原始对象的方法在被调用之前被绑定到另一个对象。这在事件处理程序中相当常见,例如,
This
通常(但不总是)最终成为事件目标,而不是方法的原始对象

开发人员通常使用闭包来确保他们对原始
this
有一个安全的引用。例如,您可以在一个表示
var self=this的构造函数中定义
colonize
,这将保证名称
self
指向原始
this
,即使
this
本身也会反弹


另一种方法是使用(对于旧的JS引擎,您必须使用polyfill),它使用
这个
对象创建一个新函数,该对象保证是您指定的任何对象。

听起来像是从DOM窗口而不是本地类调用函数。当
这个
对象是一个窗口时,不可避免地会出现范围问题

您的问题在于
setTimeout
函数。调用超时时,它告诉
DOMWindow
,而不是本地类调用函数。要解决此问题,请将调用包装到函数中。
function(){
}

编辑:我不确定setTimeout中额外字段的用途,所以我省略了我的解决方案。如果你把你正在做的事情包装到一个函数中,它应该可以工作。

change

this.colonizingTimer = setTimeout(this.colonize, ms,layer, angle + 5, color,ms);

问题是,由于超时,
对象将从对象范围中删除,并在执行时引用全局对象(
窗口
),该对象没有名为
clearLayer
的方法


这里有一个例子来说明区别。&最正确的方法是使用“延迟”:

但是,如果我理解正确的话,你想把角度从零度设置为360度吗?为什么不使用“动画化”和“动画化”


关于LibCanvas的每一个问题,你都可以给我发一封电子邮件shocksilien@gmail.com

这不应该有什么区别,因为self在评估时等于这个值。
var self = this;
this.colonizingTimer = setTimeout(function(){self.colonize.call(self);}, ms,layer, angle + 5, color,ms);
this.colonizingTimer = this.colonize.delay(ms, this, [layer, angle + 5, color, ms]);