Sja*_*sma 9 javascript coffeescript
我在使用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) )
Run Code Online (Sandbox Code Playgroud)
正如您在上面的代码底部所看到的,我试图一次又一次地调用我们所在的函数.但问题是我不能@drawFirstLine在另一个函数内部使用(requestAnimationFrame函数).在简单的JavaScript中我可以使用var self = this并引用自我.但是有人知道如何在coffeescript中处理这个问题吗?
提前致谢,
Ale*_*yne 18
requestAnimationFrame( => @drawFirstLine(currentAngle / 100) )
Run Code Online (Sandbox Code Playgroud)
编译为:
var _this = this;
requestAnimationFrame(function() {
return _this.drawFirstLine(currentAngle / 100);
});
Run Code Online (Sandbox Code Playgroud)
它基本上是self = this为你做的this,@在函数内部或在函数内部this声明函数的时候.它非常方便,它可能是我最喜欢的coffeescript功能.