繁体   English   中英

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

[英]How to call function from another function inside the same class

我是javascript新手,无法弄清楚如何解决此问题。 我目前正在尝试创建一个个人微博程序,以在新的Twitter上“提醒”我,查看新的关注者等。我正在使用node.js和npm软件包Twit编写此bot。 我目前正在尝试创建一个创建新推文的函数。 我的问题是,如果我尝试运行代码,则会从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.' });

我的班级文件

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到窗口对象。 如果改用() => { } ,则this引用应保留您希望的值(例如,您的类)。

这是一组有关箭头功能的文档。 尤其要注意箭头函数在处理thissuper类的变量方面的区别。

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

您可以通过绑定传递给this.T.post()的回调函数或使用箭头函数来避免这种情况。 tweet方法更改为此:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM