我正在尝试使我的应用程序更加面向对象。为此我想创建以下类(对象):
在 Media.js 文件中
//Media object
var Media = function (file, targetDirectory) {
this.file = file;
this.targetDir = targetDirectory;
this.fileName = this.getName();
};
Media.prototype.isVideo = function () {
return this.file.mimetype.indexOf('video') >= 0;
};
Media.prototype.isAudio = function () {
return this.file.mimetype.indexOf('audio') >= 0;
};
Media.prototype.getName = function () {
return this.file.originalname.substr(0, this.file.originalname.indexOf('.'))
};
我希望在应用程序的多个位置使用此对象,但我不太确定如何包含它。这也是个好主意还是我应该使用 Modules
代替?
最佳答案
您可以在 Media.js 中导出 Media 对象,如下所示
module.exports = Media;
然后在需要时只需需要 Media.js
const Media = require('pathtofile/Media.js')
Media.isAudio(a, b);
关于javascript - Node.js 类作为模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111872/