如何从同一个类中的另一个函数调用函数

Tru*_*zze 4 javascript node.js

我是 javascript 新手,不知道如何解决这个问题。我目前正在尝试创建一个小的个人推特机器人,它会在新推文中“提醒”我,查看新的关注者等。我正在用 node.js 和 npm 包 Twit 编写这个机器人。我目前正在尝试创建一个创建新推文的函数。我的问题是,如果我尝试运行我的代码,我会收到来自 node.js 的错误。

这是我的 bot.js 文件

var config = require("./app/config");
var TwitterBot = require("./app/classes/TwitterBot");
var Twit = require("twit");

var T = new Twit(config);
var app = new TwitterBot(T);

app.tweet({ text: 'This is my test Tweet.' });
Run Code Online (Sandbox Code Playgroud)

我的班级档案

module.exports = class TwitterBot {
  constructor(T) {
    this.T = T;
    this.colors = require('colors/safe');
    console.log("Class works");
  }

  tweet(data) {
    this.T.post(
      'statuses/update',
      { status: data.text },
      function(err, data, response) {
        if (err) {
          this.error("Something went wrong");
        } else {
          this.success("Tweet successfully created");
        }
      }
    );
  }

  error(something) {
    console.log("Error: " + something);
    // This is just an example in my code it performs a switch function
  }

  success(something) {
    console.log("Success: " + something);
    // This is just an example in my code it performs a switch function
  }

}
Run Code Online (Sandbox Code Playgroud)

我的错误:

Class works
C:\Users\Colin\Desktop\Twitter-Bot\app\classes\TwitterBot.js:14
          this.error('tweet', err);
              ^

TypeError: Cannot read property 'error' of undefined
    at C:\Users\Colin\Desktop\Twitter-Bot\app\classes\TwitterBot.js:14:15
    at C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:118:13
    at onRequestComplete (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:324:7)
    at Request.<anonymous> (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:341:7)
    at emitOne (events.js:101:20)
    at Request.emit (events.js:188:7)
    at Gunzip.<anonymous> (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\request\request.js:1001:12)
    at Gunzip.g (events.js:291:16)
    at emitNone (events.js:91:20)
    at Gunzip.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法从我的 TwitterBot 类中的所有推文函数调用函数(错误或成功)。我习惯在 php 中编写这样的代码

  public function tweet($text) {
    // ... Some function that sends the tweet and returns ($err, $data, $response)
    if ($err) {
      // I call other functions inside the same class with $this->functionName();
      $this->error($err);
    }
  }

  protected function error($err) {
    // do something with the $err variable
  }
Run Code Online (Sandbox Code Playgroud)

但在 javascript 中,它似乎不是这样工作的。有人可以帮我解决这个问题吗?

fur*_*kle 5

使用箭头函数。function() { }符号的问题之一是它更改this为 window 对象。如果您() => { }改为使用,则this引用应保持为您希望的值(例如您的类)。

这是一组关于箭头函数的文档。请特别注意箭头函数在处理类变量(如this和 )方面有何不同的参考super

唯一可能的缺点是箭头函数在 ES6 之前的语言环境中不起作用,因此如果您的项目需要广泛的浏览器支持,您可能需要使用像 Babel 这样的转译器。鉴于它是一个服务器端应用程序,我怀疑这会成为一个问题。