我正在使用 NodeJS 并打算编写一些实用程序函数。我在这里想到了两种选择。
第一个是传统的方法,即
module.exports = {
random: () => Math.random(),
};
Run Code Online (Sandbox Code Playgroud)
第二种选择是将 ES6 类与静态方法一起使用,例如
class MyMath {
static random() {
return Math.random();
}
}
module.exports = MyMath;
Run Code Online (Sandbox Code Playgroud)
从编程/单元测试的角度来看,哪一个更好?或者它们几乎相同,因为 ES6 类本质上是一种语法糖?
更新:感谢评论的人。我看到那些问类静态方法与实例方法,或原型方法与对象方法的问题,但我的更像是类静态方法与对象方法。
项目组架构:
var ProjectGroupSchema = new Schema({
projectGroupId : String,
title : String
});
Run Code Online (Sandbox Code Playgroud)
项目架构:
var ProjectSchema = new Schema({
title : {type : String, default : '', required : true},
group : {type: String, ref: 'ProjectGroup' },
subscribers : [{type: String, ref: 'User' }]
});
Run Code Online (Sandbox Code Playgroud)
用户架构:
var UserSchema = new Schema({
userId : {type: String, require: true},
firstName : {type: String, required: true},
lastName : {type: String, required: true},
});
Run Code Online (Sandbox Code Playgroud)
然后,我可以进行以下填充:
project.findById(req.projectId})
.populate('subscribers')
.populate('group')
.exec(function(err, …Run Code Online (Sandbox Code Playgroud)