我对 js 或 coffeescript 有点陌生,我不知道我的脚本有什么问题。
class Token
fetching_token = $.Deferred()
assigning_token = $.Deferred()
constructor: ->
@token = null
@got_token = $.Deferred()
fetch = ->
fetching_token.resolve({"access_token": '12355'})
assign_token = (data) =>
console.log "TOKEN (instance var): " + @token #undefined?
@token = data.access_token
assigning_token.resolve()
get_token: ->
fetch()
$.when(fetching_token).done (data) =>
assign_token(data)
$.when(assigning_token).done =>
@got_token.resolve()
undefined
t = new Token
t.get_token()
$.when(t.got_token).done ->
console.log "FETCHED TOKEN: " + t.token #gives null
我试图在对象实例上公开以下接口:token,got_token,get_token。由于某种原因,assign_token中的@token是undefined。我尝试了一些与胖箭头的组合,但无法正常工作。先感谢您