Javascript CoffeeScript函数作为node.js API中的参数
我正在用CoffeeScript为Atom编写一个插件,我正在使用。它有一些非对称函数,所以我必须将函数作为参数传递,这些参数被称为回调函数。我的问题是,当我使用以下代码时:Javascript CoffeeScript函数作为node.js API中的参数,javascript,node.js,coffeescript,atom-editor,Javascript,Node.js,Coffeescript,Atom Editor,我正在用CoffeeScript为Atom编写一个插件,我正在使用。它有一些非对称函数,所以我必须将函数作为参数传递,这些参数被称为回调函数。我的问题是,当我使用以下代码时: login: -> console.log 'Loging in' @client = telegramLink.createClient({ id: config.telegram.prod.app.id, hash: config.telegram.prod.app
login: ->
console.log 'Loging in'
@client = telegramLink.createClient({
id: config.telegram.prod.app.id,
hash: config.telegram.prod.app.hash,
version: '0.0.1',
lang: 'en'
},
config.telegram.prod.primaryDataCenter);
@client.createAuthKey(@authCallback)
console.log @client
authCallback: (auth) ->
console.log auth
@client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback)
它被编译为:
login: function() {
console.log('Loging in');
this.client = telegramLink.createClient({
id: 12345,
hash: 'q1w2e3r4t5y6u7i8o9p0',
version: '0.0.1',
lang: 'en'
}, config.telegram.prod.primaryDataCenter);
this.client.createAuthKey(this.authCallback);
return console.log(this.client);
},
authCallback: function(auth) {
console.log(auth);
return this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', this.sendCodeCallback);
}
authCallback函数中未定义@client
我在Stackoverflow()上读到我应该使用=>
(胖箭头),所以我尝试了这个方法,得到了以下编译脚本:
authCallback: (function(_this) {
return function(auth) {
console.log(auth);
return _this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', _this.sendCodeCallback);
};
})(this)
但是@client
仍然没有定义。我认为API中的回调函数调用可能不再正常工作
我还能做些什么来保持原来的作用域,但要使它与API一起工作?当我查看代码的索引时,我猜您不在类中。如果您不在类的实例中,“this”或“@”将不起作用。第二件事是回调函数的定义。不应将其定义为类的方法。该类可以如下所示:
class MyClass
constructor: ->
// do something
login: ->
@client = telegramLink.createClient({
id: config.telegram.prod.app.id,
hash: config.telegram.prod.app.hash,
version: '0.0.1',
lang: 'en'
@client.authCallback: (auth) =>
console.log auth
@client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback)
如果你的类工作正常,你需要创建一个实例,所以这可能不是你想要的
您可以更改代码,使其工作方式与仅使用函数定义的一样
login: ->
console.log 'Loging in'
client = telegramLink.createClient({
id: config.telegram.prod.app.id,
hash: config.telegram.prod.app.hash,
version: '0.0.1',
lang: 'en'
},
config.telegram.prod.primaryDataCenter);
client.createAuthKey(@authCallback)
console.log @client
authCallback: (auth) ->
console.log auth
client.auth.sendCode(config.telegram.test.telNr, 5, 'en', sendCodeCallback)
提示:createClient方法中的选项字典的定义看起来像JS,而不像Coffescript。也不应该使用“;”。
混用这些定义也可能会产生一些错误。解决方案:
今天我找到了解决这个问题的办法。
现在我的代码看起来是这样的:
login: ->
console.log 'Logging in'
@client =
telegramLink.createClient({
id: config.telegram.prod.app.id
hash: config.telegram.prod.app.hash
version: '0.0.1'
lang: 'en'
}
config.telegram.prod.primaryDataCenter)
@client.createAuthKey((auth) =>
console.log @client.auth
@client.auth.sendCode(
config.telegram.test.telNr,
5,
'en',
@sendCodeCallback))
console.log @client
如何调用
authCallback
?请记住,在(Java | Coffee)脚本函数中,此
取决于函数的调用方式,而不是函数的定义方式或定义位置(当然,绑定函数除外)。使用=>
应该可以做到这一点。您是否查看了@
中的authCallback
?您确定要先调用login
?确实要在同一对象上调用login
和authCallback
吗?@muistooshort来自API的源代码if(callback){this.client.once('sendCode',callback);}
。API是用纯nodejs编写的,而不是用CoffeeScript编写的,因此根本没有@
。是的,我肯定我先调用了login,因为它是由Atom中的一个菜单按钮触发的。如果您想查看我在@
上共享的整个项目代码,这是CoffeeScript对的缩写,因此@
就在这里。我不知道这个.client.once('sendCode',callback)
做了什么,所以你必须深入挖掘。或者您可以console.log(@)
在authCallback
中登录并亲自查看。请回答,而不是问题的一部分:)谢谢您的回答。我的代码在一个类中。感谢JS和CoffeeScript的混合提示。我只是从API的文档中复制了它。