什么时候用这个代替原型是合适的?

function A() { this.foo = function() { return "bar"; }; } A.prototype.foo = function() { return "bar"; }; 

我主要是通过添加“实例方法”的第二种方法。 主要是因为习惯,这是一个小记忆保护程序。 但是什么时候使用第一种方法是合适的呢?

注意:这个问题以前几乎被问过,但是他们大多是谈论不同的,这个问题更多的是什么时候使用它们。

你可以使用this私有variables的方法。

例:

 function Counter() { var c = 0; this.getCount = function() { return c; }; this.increase = function() { c++; }; } 

没有办法通过原型方法让函数访问variables,也不会将其暴露给其他人。