CoffeeScript函数作为node.js API中的参数
CoffeeScript function as parameter in node.js API
我正在CoffeeScript中为Atom编写一个插件,我正在使用这个nodejs API(telegram.link)。它有一些非对称函数,所以我必须将函数作为参数传递,这些参数被调用为回调。我的问题是,当我使用以下代码时:
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(CoffeeScripts类-在回调中访问属性)上读到我应该使用=>
(胖箭头),所以我尝试了一下,得到了以下编译脚本:
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
相关文章:
- 回调函数中传递参数的困难(Google Map API Markers)
- 在Flickr API的标签参数中传递一个变量
- 如何正确地包含带有参数的谷歌地图API
- Json 参数强制转换继承 Web API 2.
- 在 Google 云端硬盘 API 请求中使用参数
- 使用Angular Resource查询具有字符串参数的REST API
- 如何使用JQueryUI滑块控制web音频API振荡器参数,而无需经过HTML表单参数
- 使用不同的API参数更新DIV内容
- 如何使用URL参数将地址传递到javascript API映射中
- 正在使用字段参数查询FB Graph API提要
- 如何使用AngularJS将多个参数发送到JAVA API
- Web Api 2属性路由和Angular JS参数
- 谷歌地图API v3-缩放参数don'不起作用
- 如何添加id和额外的参数到一个放大弹出iframe与youtube视频,所以我可以控制与API youtube播放器
- Facebook Javascript API调用返回需要appsecret_proff参数的错误,但这是客户端
- 将查询字符串参数与谷歌地图api v3服务结合使用
- 问题将 Javascript 数组发送到带有 List
参数的 Web API 方法 - Web音频API参数调制在Safari中不工作
- Facebook营销API:参数必须是一个有效的Instagram帐户id
- Web API参数路径过长