微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – var self = 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) )

正如你可以在上面的代码底部看到的,我试图一次又一次地调用这个函数.但问题是我不能在另一个函数(requestAnimationFrame函数)中使用@drawFirstLine.在简单的javascript中,我可以使用var self = this并引用自己.但是有人知道如何在咖啡书中处理这个问题吗?

提前致谢,

解决方法

Use the fat arrow.
requestAnimationFrame( => @drawFirstLine(currentAngle / 100) )

其编译为:

var _this = this;

requestAnimationFrame(function() {
  return _this.drawFirstLine(currentAngle / 100);
});

它基本上是为你自己,这使得这个或@在函数内部,当这个函数被声明时是什么.这很方便,这可能是我最喜欢的咖啡书.

原文地址:https://www.jb51.cc/js/154226.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐