gpt4 book ai didi

javascript - CoffeeScript 类字段未定义

转载 作者:行者123 更新时间:2023-11-30 09:01:00 25 4
gpt4 key购买 nike

这段代码传入了一个函数,并没有继承我的 EmployeesController 对象的状态。我该怎么做才能将我的 EmployessController 对象绑定(bind)到焦点事件?

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


searchInputGainedFocus: ->
console.debug @dateInput

换句话说,当我给 dateInput 焦点时,console.debug 打印未定义。

最佳答案

使用 "fat arrow" (=>)searchInputGainedFocus 绑定(bind)到对象:

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 is helpful when using callback-based libraries like Prototype or jQuery, [...]

所以像这样定义searchInputGainedFocus:

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

searchInputGainedFocus: =>
console.debug @dateInput

关于javascript - CoffeeScript 类字段未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9319746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com