我喜欢javascript和Node,但是当我声明对象时......我不知道什么是最佳实践和高性能。我感兴趣的是高性能。
"use strict";
module.exports = SignUpBusiness;
SignUpBusiness.__proto__ = new SignUpBusiness;
function SignUpBusiness(){
this._certificate = null;
this._db = require('mongoose');
this._model = require('../models/User.js');
this._userModel = this._db.model('User', this._model.UserSchema);
this._result = false;
this.runLoginProcess = function(credentials , next){
//code
}
// etc.
}
或者这样:
"use strict";
module.exports = {
_certificate : null,
_db : require('mongoose'),
_model : require('../models/User.js'),
_userModel : this._db.model('User', this._model.UserSchema),
_result : false,
runLoginProcess: function(credentials , next){
//code
}
// etc.
}
非常感谢! 而且...对不起我的英语。
答案 0 :(得分:0)
使用原型(第一选择)是最好的方法。原型是最佳实践并减少内存消耗(否则您将重新声明每个新对象的内存)。
根据路易斯的说法,上述情况可能不准确:
如果将模块的导出分配给foo,则定义为foo。 proto ._ proto _。 proto ,但是foo。< strong> proto ._ proto _。 proto ._ proto _不是。第二个选择的深度为1:foo。 proto 已定义,但不是更深。第一种情况不仅占用更多内存,而且速度也慢,因为字段查找必须遍历更深的链。
我现在无法对自己的答案进行测试。我会稍后再试。