【发布时间】: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