我写在CoffeeScript中的原子的插件,我使用的是this nodejs API (telegram.link)。它有一些不对称函数,所以我必须将函数作为参数传递,称为回调函数。我的问题是,当我使用下面的代码:CoffeeScript的功能参数Node.js的API
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);
}
@client
在authCallback功能是不明确的。
我读#2(CoffeeScripts classes - access to property in callback),我应该用=>
(脂肪箭头),所以我在下面的编译脚本试图由此产生:
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?
如何调用authCallback?请记住,'this'在(Java |咖啡)脚本函数取决于函数是如何被调用,而不是如何或在哪里它被定义(除了当然的约束功能)。使用'=>'应该可以做到。你看过'authCallback'里面有什么'@'吗?你确定你首先调用'login'吗?你确定你在同一个对象上调用'login'和'authCallback'吗? –
@ muistooshort从API的源代码'if(callback){this.client.once('sendCode',callback); }'。 API是用纯nodejs编写的,而不是在CoffeeScript中,所以根本就没有'@'。是的,我确定我首先调用了登录,因为它是由Atom中的菜单按钮触发的。如果你想看到我的整个项目代码,我已经在[Github]上分享了它(https://github.com/Morosko/atom-telegram) – Morosko
'@'是CoffeeScript对'this'的简写,所以'@'在那里。我不知道'this.client.once('sendCode',callback)'做了什么,所以你不得不深入挖掘。或者你可以在'authCallback'中'console.log(@)'并亲自查看。 –