【问题标题】:NodeJS using a class from another fileNodeJS 使用另一个文件中的类
【发布时间】:2018-10-23 17:00:39
【问题描述】:

我有一个 java 脚本文件,它正在引用另一个包含使用

的类的 javascript 文件
const Champion = require("./championgg_webscraper_cheerio.js");

然后我尝试通过

实例化 Champion 类的对象
var temp = new Champion("hello");
console.log(temp);

当我这样做时,它会将其打印到控制台,指示和未定义的变量:

Champion {}

此外,当我尝试打印未定义的类的属性时,我认为它可能无法访问 most_frequent_completed_build 变量。

console.log(temp.most_frequent_completed_build);

这里是 Championgg_webscraper_cheerio.js 文件

function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
};
module.exports = Champion;

【问题讨论】:

  • 你能试试const Champion = require("./championgg_webscraper_cheerio.js").Champion;吗?
  • 我已将 Champion 的声明修改为:const Champion = require("./championgg_webscraper_cheerio.js").Champion; 现在当我尝试时,Champion 不是构造函数:var temp = new Champion("hello");

标签: javascript node.js class oop module


【解决方案1】:

你正在导出一个函数

你所要做的就是调用那个函数

var temp = Champion();

您可以阅读更多关于new关键字herehere的信息

【讨论】:

  • 我已将 temp 的声明更改为 var temp = Champion("hello"); 并且遇到了 Cannot read property most_frequent_completed_build of undefined
  • 是的。它不会因为most_frequent_completed_build 的范围在该函数内。如果需要,您可以从函数中返回此值。
  • 根据我对Java中对象的理解,如果我有一个在该类中有变量的类,那么Class对象将能够看到它自己的变量。我试图让 Champion 像一个有两个公共数组 most_frequent_completed_buildhighest_win_percentage_completed_build 的类一样工作,这些数组设置为给定 url 提供的信息。是否可以从 Champion 构造函数返回 Champion 对象?
  • 很遗憾,您无法与 Java 相提并论。例如,“this”键完全不同。而且你的代码在异步模式下运行,所以即使你到达这个变量,它仍然是未定义的
  • 是的,我想我在调试时意识到了这一点。有没有办法在我们将它们分配回对象之前等待请求函数完成。
【解决方案2】:

我认为您需要一个名为 ChampionFunction 构造函数(类似于 Java 等其他编程语言中的类的原型或蓝图)。

作为替代方案,我建议您学习ES6 way of writing classes,它类似于Java。

您可以通过将所有变量或方法添加到函数构造器内的 this 变量中来实现这一点,以便 您可以使用使用创建的对象访问它们'new' 关键字,即使它们成为类成员或方法。

在你的情况下,

function Champion(champName) {
    //Some code

    this.most_frequent_completed_build = NULL;

    //Rest of code
}

module.exports = Champion;

只要确保每当您尝试访问类变量时,始终使用this.variable_name,如this.most_frequent_completed_build

因此,当您在主应用程序中创建该类的新对象时,您将能够访问所有类成员和方法。

const Champion = require("./championgg_webscraper_cheerio.js");

var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);

【讨论】:

    【解决方案3】:
        function Champion(champName) {
      //CHEERIO webscraping
      var cheerio = require('cheerio');
      //REQUEST http library
      var request = require('request');
      //url of the champion
      var url = "http://champion.gg/champion/Camille/Top?";
      var most_frequent_completed_build;
      var highest_win_percentage_completed_build;
      request(url,
        function(error, response, html) {
          if (!error && response.statusCode == 200) {
            var $ = cheerio.load(html);
            var final_build_items = $(".build-wrapper a");
            var mfcb = [];
            var hwpcb = [];
            for (i = 0; i < 6; i++) {
              var temp = final_build_items.get(i);
              temp = temp.attribs.href;
              //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
              temp = temp.slice(38);
              mfcb.push(temp);
            }
            for (i = 6; i < 12; i++) {
              var temp = final_build_items.get(i);
              temp = temp.attribs.href;
              //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
              temp = temp.slice(38);
              hwpcb.push(temp);
            }
            most_frequent_completed_build = mfcb;
            highest_win_percentage_completed_build = hwpcb;
          } else {
            console.log("Response Error: " + response.statusCode);
          }
        }
      );
      return {most_frequent_completed_build:most_frequent_completed_build};
    };
    module.exports = Champion;
    
        var temp = new Champion("hello");
    console.log(temp.most_frequent_completed_build);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多