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

CoffeeScript function as parameter in node.js API

我正在使用 CoffeeScript 为 Atom 编写插件,并且正在使用 this 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);
}

@client 在 authCallback 函数中未定义。

我在 Whosebug (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 一起使用?

当我查看您的代码索引时,我猜您不在 class 中。如果您不在 class "this" 的实例中,则“@”将不起作用。第二件事是回调的定义。它不应被定义为您 class 的方法。 class 可能看起来像这样:

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)

为了让您的 class 工作正常,您需要创建一个实例,所以这可能不是您想要的。

您可以更改您的代码,使其像您定义的函数一样工作。

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