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

标签 javascript node.js

我是 javascript 的新手,不知道如何解决这个问题。我目前正在尝试创建一个小型个人 Twitter 机器人,它会在发布新推文时“提醒”我,查看新的关注者等。我正在使用 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.' });

我的类(class)文件

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
  }

}

我的错误:

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)

我的问题是我无法在我的类 TwitterBot 中调用我的 tweet 函数中的函数(错误或成功)。我习惯在 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
  }

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

最佳答案

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

这是关于 arrow functions 的一组文档.请特别注意箭头函数在处理类变量方面的不同之处,例如 thissuper

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

关于javascript - 如何从同一个类中的另一个函数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41815558/

相关文章:

node.js - 如何使用 sequelize 播种 uuidv4

angularjs - Prerender.io 不缓存我的页面

javascript - 如何捕获选择选择框上的悬停事件

javascript - 在 Polymer 中,样式应该在什么时候位于模板标签的内部或外部?

javascript - 无法在检查器/开发工具中选择 Chrome 扩展程序/代码环境

javascript - 在一个函数中传递一个参数,该参数在另一个函数中通过 "reference"传递

node.js - 即使我在 config.json 的外部部分中定义,jQuery 也不会作为模块加载

node.js - 将 Node.js headless 浏览器与 selenium 混合使用?

node.js - Mongodb toArray() 性能

javascript - 有没有办法覆盖 Olark css?