CoffeeScript函数作为node.js API中的参数

CoffeeScript function as parameter in node.js API

本文关键字:API 参数 js node 函数 CoffeeScript      更新时间:2023-09-26

我正在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