我有以下情况。我正在定义一个新类C
并将其导出为node
模块。我想知道使用普通函数(method 1
)与使用原型方法(method 2
)之间的区别。使用原型方法与普通函数的区别
似乎没有全局命名空间污染问题(因为它是一个模块内)与method 1
的帮手,但实际上method 2
通过原型继承暴露了一些内部的东西(这里的helper
方法)到外部。
任何其他差异(性能等)?我应该正常使用哪一个?
C.prototype.func=function(){
// need to call helper, use either
// helper();
// or
// this.helper();
}
function helper(){// method 1
}
C.prototype.helper=function(){// method 2
}
module.exports = C;
仅供参考,你不原型喜欢你的表现定义功能。这将是'C.prototype.helper = function(){...}' – jfriend00 2014-12-19 06:29:49