【问题标题】:CoffeeScript function as parameter in node.js APICoffeeScript 函数作为 node.js API 中的参数
【发布时间】:2015-11-10 18:02:41
【问题描述】:

我正在用 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 函数中未定义。

我在 Stackoverflow (CoffeeScripts classes - access to property in callback) 上读到我应该使用 => (fat-arrow) 所以我尝试了这个导致以下编译脚本:

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 是如何被调用的?请记住,(Java|Coffee)Script 函数中的this 取决于函数的调用方式,而不是定义的方式或位置(当然绑定函数除外)。使用=> 应该可以解决问题。你看过@authCallback里面是什么吗?你确定你先打电话给login 吗?你确定你在同一个对象上调用loginauthCallback
  • @muistooshort 来自 API 的源代码 if (callback) { this.client.once('sendCode', callback); }。 API 是用纯 nodejs 而不是 CoffeeScript 编写的,所以根本没有 @。是的,我确定我首先调用了登录,因为它是由 Atom 中的菜单按钮触发的。如果您想查看我的整个项目代码,我已在 Github 上分享了它
  • @ 是 CoffeeScript 对 this 的简写,所以 @ 就在那里。我不知道this.client.once('sendCode', callback) 做了什么,所以你必须深入挖掘。或者你可以在authCallbackconsole.log(@) 自己看看。
  • 请回答问题,而不是问题的一部分:)

标签: javascript node.js coffeescript atom-editor


【解决方案1】:

当我查看你的代码时,我猜你不在一个类中。如果您不在类的实例中,“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。还有“;”你不应该使用。 混合定义也会产生一些错误。

【讨论】:

  • 感谢您的回答。我的代码在课堂上。感谢您提供 JS 和 CoffeeScript 混合的提示。我刚刚从 API 的文档中复制了它。
【解决方案2】:

解决方案:

今天我找到了解决问题的方法。 现在我的代码如下所示:

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

【讨论】:

    猜你喜欢
    • 2011-10-27
    • 2015-03-01
    • 1970-01-01
    • 2013-06-10
    • 2012-06-28
    • 1970-01-01
    • 2012-11-28
    • 2015-02-11
    • 2011-10-06
    相关资源
    最近更新 更多