Node.js 无法在类中使用类外变量
我正在制作一个简单的笔记应用程序来学习node和ES6。我有3个模块-应用程序、NotesManager和Note。我正在将Note类导入NotesManager,并尝试在其addNote函数中实例化它。问题是,即使导入是正确的,但在类定义中却没有定义。一个更简单的解决方案是实例化NotesManager类并将Note类添加到其构造函数中。然而,我希望将NotesManager作为静态实用程序类 这是我的密码。 Note.jsNode.js 无法在类中使用类外变量,node.js,ecmascript-6,Node.js,Ecmascript 6,我正在制作一个简单的笔记应用程序来学习node和ES6。我有3个模块-应用程序、NotesManager和Note。我正在将Note类导入NotesManager,并尝试在其addNote函数中实例化它。问题是,即使导入是正确的,但在类定义中却没有定义。一个更简单的解决方案是实例化NotesManager类并将Note类添加到其构造函数中。然而,我希望将NotesManager作为静态实用程序类 这是我的密码。 Note.js class Note { constructor(title,
class Note {
constructor(title, body) {
this.title = title;
this.body = body;
}
}
module.exports = Note;
const note = require("./Note");
console.log("Note: ", note); //shows correctly
class NotesManager {
constructor() {}
static addNote(title, body) {
const note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(note);
}
static getNote(title) {
if (title) {
console.log(`Getting Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static removeNote(title) {
if (title) {
console.log(`Removing Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static getAll() {
//console.log("Getting all notes ", NotesManager.notes, note);
}
}
NotesManager.notes = []; //Want notes to be a static variable
module.exports.NotesManager = NotesManager;
console.log("Starting App");
const fs = require("fs"),
_ = require("lodash"),
yargs = require("yargs"),
{ NotesManager } = require("./NotesManager");
console.log(NotesManager.getAll()); //works
const command = process.argv[2],
argv = yargs.argv;
console.log(argv);
switch (command) {
case "add":
const title = argv.title || "No title given";
const body = argv.body || "";
NotesManager.addNote(title, body); //Fails here
break;
case "list":
NotesManager.getAll();
break;
case "remove":
NotesManager.removeNote(argv.title);
break;
case "read":
NotesManager.getNote(argv.title);
break;
default:
notes.getAll();
break;
}
NotesManager.js
class Note {
constructor(title, body) {
this.title = title;
this.body = body;
}
}
module.exports = Note;
const note = require("./Note");
console.log("Note: ", note); //shows correctly
class NotesManager {
constructor() {}
static addNote(title, body) {
const note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(note);
}
static getNote(title) {
if (title) {
console.log(`Getting Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static removeNote(title) {
if (title) {
console.log(`Removing Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static getAll() {
//console.log("Getting all notes ", NotesManager.notes, note);
}
}
NotesManager.notes = []; //Want notes to be a static variable
module.exports.NotesManager = NotesManager;
console.log("Starting App");
const fs = require("fs"),
_ = require("lodash"),
yargs = require("yargs"),
{ NotesManager } = require("./NotesManager");
console.log(NotesManager.getAll()); //works
const command = process.argv[2],
argv = yargs.argv;
console.log(argv);
switch (command) {
case "add":
const title = argv.title || "No title given";
const body = argv.body || "";
NotesManager.addNote(title, body); //Fails here
break;
case "list":
NotesManager.getAll();
break;
case "remove":
NotesManager.removeNote(argv.title);
break;
case "read":
NotesManager.getNote(argv.title);
break;
default:
notes.getAll();
break;
}
App.js
class Note {
constructor(title, body) {
this.title = title;
this.body = body;
}
}
module.exports = Note;
const note = require("./Note");
console.log("Note: ", note); //shows correctly
class NotesManager {
constructor() {}
static addNote(title, body) {
const note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(note);
}
static getNote(title) {
if (title) {
console.log(`Getting Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static removeNote(title) {
if (title) {
console.log(`Removing Note: ${title}`);
} else {
console.log("Please provide a legit title");
}
}
static getAll() {
//console.log("Getting all notes ", NotesManager.notes, note);
}
}
NotesManager.notes = []; //Want notes to be a static variable
module.exports.NotesManager = NotesManager;
console.log("Starting App");
const fs = require("fs"),
_ = require("lodash"),
yargs = require("yargs"),
{ NotesManager } = require("./NotesManager");
console.log(NotesManager.getAll()); //works
const command = process.argv[2],
argv = yargs.argv;
console.log(argv);
switch (command) {
case "add":
const title = argv.title || "No title given";
const body = argv.body || "";
NotesManager.addNote(title, body); //Fails here
break;
case "list":
NotesManager.getAll();
break;
case "remove":
NotesManager.removeNote(argv.title);
break;
case "read":
NotesManager.getNote(argv.title);
break;
default:
notes.getAll();
break;
}
我是否可以创建一个严格的实用程序类,而不用像Java那样实例化它?这里很新,我尝试过在没有任何运气的情况下搜索它。感谢您的帮助。执行此操作时:
const note = new note(title, body);
您可以重新定义note
从外部范围隐藏原始note
。您需要选择一个不同的变量名
这样的方法应该更有效:
static addNote(title, body) {
const some_note = new note(title, body); //Fails here as note is undefined
NotesManager.notes.push(some_note);
}
我实际上不知道问题是什么,但没有理由将
NotesManager
作为一个类。您可以直接从模块中导出函数。此外,有一个惯例,构造函数或类名应大写,如Note
中所述,以直观地将其与常规函数分开。当您执行const note=require(“./note”)时修复该问题代码>然后更改为const note=新注释(标题、正文)
也会解决您的问题,因为note
实例与note
类是分开的。我同意。这是我正在做的教程中的一个练习。他们希望以上所有内容都是课程。谢谢@jfriend00的建议。从今以后我会记住这一点,这很公平。java类实用工具类肯定是JS中的反模式,以后的注释。非常感谢快速响应。“我真不敢相信我错过了。”里夫纳特马尔萨有时你只需要一双新眼睛。