Tôi đang xử lý một số vấn đề phạm vi trong khi sử dụng Coffeescript.var self = this in 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))
Như bạn có thể thấy ở dưới cùng của mã trên, tôi đang cố gắng gọi hàm mà chúng tôi đang ở trong, một lần nữa và một lần nữa. Nhưng vấn đề là tôi không thể sử dụng @drawFirstLine
bên trong một hàm khác (hàm requestAnimationFrame). Trong javascript đơn giản, tôi có thể sử dụng var self = this
và tự giới thiệu. Nhưng có ai biết làm thế nào để đối phó với điều này trong coffeescript?
Cảm ơn trước,
có thể trùng lặp của [Đối tượng tham chiếu lớp trong một hàm lồng nhau] (http://stackoverflow.com/questions/18281886/trouble-referencing-class-object-inside-a-nested-function) – Mathletics