模块化模式vs原型 – Nodejs?
我在Nodejs
工作。 我曾经在modular pattern
。 编码简单易行。
注意
我的同事告诉Prototype模式是Nodejs和Modular模式最好的方法。
我的代码示例
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
和扩展function,并且不pipe对象的数量如何,内存中只有一个函数实例。 Vinoth.prototype.log
添加到prototype
并且不会为新对象再次创build此函数。
在Modular pattern
,为每个对象创build一个新的函数实例,但可以帮助您进行encapsulation
。