CoffeeScripts类 - 在回调中访问属性 [英] CoffeeScripts classes - access to property in callback

查看:169
本文介绍了CoffeeScripts类 - 在回调中访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的问题。我有Foo类和在制造商我启动计时器。在计时器回调我想要alert类属性,但我会得到未定义,为什么?

I have simple problem. I have Foo class and at contructor I starting timer. In timer callback I want alert class property, but I will get "undefined", why?

class Foo
  simpleProperty: "fooBar"

  constructor: ->
    setInterval @runBar, 1 * 1000
    return

  runBar: ->
    alert @simpleProperty #undefined, why?
    return

foo = new Foo()

谢谢因为这个(或者 c>)的范围,因此

Thank you for your help!

推荐答案

@ 如果是CoffeeScript)。

Because of the scoping of this (or @ in case of CoffeeScript).

您应该使用粗箭头:

runBar: =>
  alert @simpleProperty #fooBar

查看工作 here

这篇关于CoffeeScripts类 - 在回调中访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆