【发布时间】:2017-01-03 23:58:07
【问题描述】:
我正在使用 Coffeescript 为 Atom 编辑器编写一个包。
我的示例代码:
module.exports =
class DeclarationTree
createIndexModules: (files, length) =>
@modules = new Map()
...
onClic = (text) =>
# PROBLEM **@modules** IS UNDEFINED
file_array = @modules.get(text)
console.log file_array
console.log file_array[0], file_array[1]
getProvider: ->
providerName: 'hyperclick-provider',
# getSuggestionForWord gives me a promise
getSuggestionForWord: (editor, text, range) ->
range: range, callback: ->
onClic(text)
当然,@modules 是在调用 onClic() 之前定义的。
我不知道如何解决它,我是 Javascript 的新手。
看起来onClic 和= () 定义了一个非类的方法,无法访问@module (this.module)
如何在 promise 的回调函数中访问我在类方法中创建的变量?
谢谢!
编辑:使用冒号:感谢 -1 ;)
module.exports =
class DeclarationTree
createIndexModules: (files, length) =>
@modules = new Map()
console.log this.constructor.name # DeclarationTree
...
onClic: (text) =>
# PROBLEM **@modules** IS UNDEFINED
file_array = @modules.get(text)
console.log file_array
console.log file_array[0], file_array[1]
getProvider: ->
providerName: 'hyperclick-provider',
# getSuggestionForWord gives me a promise
getSuggestionForWord: (editor, text, range) ->
range: range, callback: ->
console.log this.constructor.name # Object
@onClic(text)
获取Uncaught TypeError: this.onClic is not a function 错误。
显然,我无法在 callback 函数中访问与 this 对象相关的任何内容。
【问题讨论】:
标签: javascript coffeescript atom-editor