Let\'s say I have this code:
(function(global) {
function Bar(foo) {
this.foo = foo;
return this;
}
Bar.prototype.getFoo = funct
When you're using prototype.functionName
all instances share the same function (only one copy in memory), but if you use this.functionName
in your constructor each instance has its own copy of the same function (exists multiple times in memory).
Using prototype
has two implications:
You can also have both in which case the local copy has precedence over the prototype which means that you could do stuff like this:
function Crazy(name)
{
this.name = name;
this.callMe = function() {
return "Where are you " + this.name;
};
}
Crazy.prototype.callMe = function() {
return this.name + " come here";
};
var inst = new Crazy("Robert");
inst.callMe(); // "Where are you Robert"
delete inst.callMe;
inst.callMe(); // "Robert come here"