简体   繁体   English

CoffeeScript类字段未定义

[英]CoffeeScript class field is undefined

This code is passing in a function and is not carrying over the state of my EmployeesController object. 这段代码正在传入一个函数,并且没有继承EmployeesController对象的状态。 What can i do to bind my EmployessController object to the focus event? 如何将EmployessController对象绑定到焦点事件?

class @EmployeesController 
  constructor: (@dateInput) ->
    @dateInput.focus(@searchInputGainedFocus)


  searchInputGainedFocus: ->
    console.debug @dateInput

In other words, console.debug prints undefined when i give dateInput focus. 换句话说,当我给dateInput焦点时,console.debug打印未定义。

Use the "fat arrow" ( => ) to bind searchInputGainedFocus to the object: 使用“胖箭头”( =>searchInputGainedFocus绑定到该对象:

The fat arrow => can be used to both define a function, and to bind it to the current value of this , right on the spot. 粗箭头=>既可以用来定义函数,也可以将其绑定到this的当前值。 This is helpful when using callback-based libraries like Prototype or jQuery, [...] 当使用基于回调的库(例如Prototype或jQuery,[...]

So define searchInputGainedFocus like this: 因此,像这样定义searchInputGainedFocus

class @EmployeesController 
  constructor: (@dateInput) ->
    @dateInput.focus(@searchInputGainedFocus)

  searchInputGainedFocus: =>
    console.debug @dateInput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM