模块化模式与原型 - Nodejs?
Modular Pattern vs Prototype - Nodejs?
我在Nodejs
工作。我曾在 modular pattern
工作过。编码简单易行。
备注
My colleague told Prototype pattern is best approach for Nodejs and Modular pattern is slow.
我的代码示例
module.exports = {
get : funcntion(){
//stuff
},
set : function(){
//stuff
}
}
问题
哪种模式最适合实时 Web 应用程序或通常使用 Nodejs 的应用程序上下文?
让我把我的想法放在这里:
// vinoth.js
var Vinoth = function () {};
Vinoth.prototype.log = function () {
console.log('Hello Vinoth');
};
module.exports = new Vinoth();
// app.js
var vinoth = require('./vinoth.js');
vinoth.log();
简单module
模式
//vinoth.js
module.exports = function () {
console.log('Vinoth');
}
// app.js
var vinoth = require('./vinoth.js');
vinoth();
我 understand
是:
Prorotype pattern
帮助您 inheritance
和扩展功能,并且无论对象的数量如何,内存中只有一个函数实例。 Vinoth.prototype.log
已添加到 prototype
,并且不会为新对象再次创建此函数。
在 Modular pattern
中,对于每个对象,都会在内存中创建一个新的函数实例,但可以帮助您 encapsulation
。
我在Nodejs
工作。我曾在 modular pattern
工作过。编码简单易行。
备注
My colleague told Prototype pattern is best approach for Nodejs and Modular pattern is slow.
我的代码示例
module.exports = {
get : funcntion(){
//stuff
},
set : function(){
//stuff
}
}
问题
哪种模式最适合实时 Web 应用程序或通常使用 Nodejs 的应用程序上下文?
让我把我的想法放在这里:
// vinoth.js
var Vinoth = function () {};
Vinoth.prototype.log = function () {
console.log('Hello Vinoth');
};
module.exports = new Vinoth();
// app.js
var vinoth = require('./vinoth.js');
vinoth.log();
简单module
模式
//vinoth.js
module.exports = function () {
console.log('Vinoth');
}
// app.js
var vinoth = require('./vinoth.js');
vinoth();
我 understand
是:
Prorotype pattern
帮助您 inheritance
和扩展功能,并且无论对象的数量如何,内存中只有一个函数实例。 Vinoth.prototype.log
已添加到 prototype
,并且不会为新对象再次创建此函数。
在 Modular pattern
中,对于每个对象,都会在内存中创建一个新的函数实例,但可以帮助您 encapsulation
。