我有一个 Tag
类和一个 TagCollection
类来存储多个标签。现在 TagRepository
类需要构造一个新的 Tag
对象来返回。但是,当我尝试在 TagRepository
内创建新的 Tag
时,它会返回错误:
Tag is not defined
这就是我在主 JavaScript 文件中包含所有类的方法:
const Tag = require('./class/Tag.js');
const TagCollection = require('./class/TagCollection.js');
const TagRepository = require('./repository/TagRepository.js');
我们通常如何处理这个问题?我可以将所需的类包含在需要它们的类的构造函数中。但当我必须包含多个类时,这看起来很困惑。
我能想到的唯一其他解决方案是将所需的类设为全局,在线阅读似乎被认为是不好的做法。下面我包含了所有类(class)
Tag.js
module.exports = class Tag {
constructor() {
this.id;
this.name;
}
setId(id) {
this.id = id;
}
setName(name) {
this.name = name;
}
getId() {
return this.id;
}
getName() {
return this.name;
}
}
TagCollection.js
module.exports = class TagCollection {
constructor() {
this.tags = [];
}
addTag(tag) {
this.tags.push(tag);
}
setTags(tags) {
this.tags = tags;
}
getTags(tags) {
return this.tags;
}
}
TagRepository.js
module.exports = class TagRepository {
constructor(conn) {
this.conn = conn;
}
getAll(callback) {
let tempTagCollection = new TagCollection;
this.conn.query(`SELECT \`id\`, \`name\` FROM \`tag\` WHERE 1`, function (error, tags) {
tags.forEach((tag) => {
//Create single tag
let tempTag = new Tag;
//Set properties
tempTag.setName(tag.name);
tempTag.setId(tag.id);
//Add single tag to collection
tempTagCollection.addTag(tempTag);
})
callback(tempTagCollection);
})
}
}
最佳答案
The only other solution I could think of is making the needed classes global, reading online it seems like that is considered bad practice.
你是对的,应该尽可能避免创建全局变量,因为它会导致代码变得脆弱且难以调试。
您可以将每个文件视为一个模块。我个人喜欢为每个类保留一个文件,因此我可以将类本身视为一个模块。在每个模块中,您应该 require
您所依赖的每个类。
所以我将使用一个经典的动物/猫/狗示例:
//Animal.js
module.exports = class Animal { ... }
//Cat.js
const Animal = require('./Animal');
class Cat extends Animal { ... }
//Dog
const Animal = require('./Dog');
class Dog extends Animal { ... }
在 NodeJS 中,即使 Cat
和 Dog
都需要 Animal
,Animal.js 只会被执行一次。因此,每个需要 Animal
的模块都会获得相同的 Animal
类。
I could just include the needed classes inside the constructor of the class that requires them.
我也会避免这样做。在构造函数中使用 require
,即使 require
的文件只会在第一次需要时执行该文件,它仍然会经历节点文件解析算法,该算法这是一个昂贵的过程,并且可能会导致性能瓶颈。一般来说,最好将 require
语句放在构造函数或函数之外。将它们保留在文件的顶部,当应用程序加载时,所有的需求都将运行一次。
As you can now see inside the TagRepository it requires two classes, the Tag and TagCollection class how would I go about this?
在 TagRepository.js 中,您只需要有 2 个包含语句,每个文件一个,请参见下文。
const Tag = require('./Tag');
const TagCollection = require('./TagCollection.js');
// Both Tag and TagCollection is now usable
class TagRepository { ... }
可以在此处找到有关 NodeJS 模块的更多阅读
https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_modules
关于javascript - 在其他类中注入(inject)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718976/