Bind event in coffee script constructor

时间:2016-02-12 20:46:05

标签: javascript coffeescript

If I have class

endpoints

When I change control, change event is fired and I got error class Control constructor: (@canvas, @control) -> @control.on('keyup change', -> update(@canvas) ) .

However, if code is changed to

$canvas is undefined

everything works as expected.

Why is that?

1 个答案:

答案 0 :(得分:1)

This works as expected. Using query = oldstring.replace(" ","_") c.execute(query) on a constructor parameter automatically sets @ in our constructor. The context (this.canvas = canvas) of your event-handler is different than the class, unless you explicitly scope it with the "fat arrow" this.

=>

In your working example, you are avoiding the problem by not attaching class Control constructor: (@canvas, @control) -> @control.on('keyup change', => # notice fat arrow update(@canvas) ) to the current context.