在使用Coffeescript时,我正在处理一些范围问题.
drawFirstLine: (currentAngle) -> currentAngle = currentAngle # = 1 switch @type # set @endAngle to pick up later on # Math.PI * 2 is the endpoint of a circle divided by seconds times current seconds when "seconds" then @endAngle = Math.PI * 2 / 60 * @seconds when "minutes" then @endAngle = Math.PI * 2 / 60 * @minutes when "hours" then @endAngle = Math.PI * 2 / 24 * @hours @context.arc(@center_x,@center_y,100,@startAngle,currentAngle,@counterClockWise) @context.lineWidth = 15 console.log('drawn') text = "28px sans-serif"; @context.fillText(text,@center_x - 28,@center_y - @canvas.width / 5) @context.stroke() currentAngle++; if currentAngle < @endAngle requestAnimationFrame( -> @drawFirstLine(currentAngle / 100) )
正如你可以在上面的代码的底部看到的,我试图一次又一次地调用这个函数.但问题是我不能在另一个函数(requestAnimationFrame函数)中使用@drawFirstLine.在简单的javascript中,我可以使用var self = this并引用自己.但是有人知道如何在咖啡书中处理这个问题吗?
提前致谢,
解决方法
Use the fat arrow.
requestAnimationFrame( => @drawFirstLine(currentAngle / 100) )
其编译为:
var _this = this; requestAnimationFrame(function() { return _this.drawFirstLine(currentAngle / 100); });