簡體   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