When a function is defined in the constructor, each instance of this function is created each time the constructor is called. It also has access to private variables.
var myClass = function() {
var mySecret = Math.random();
this.name = "Fred";
this.sayHello = function() {
return 'Hello my name is ' + this.name;
};
}
When a function is defined in a prototype, a function is created only once, and one instance of this function is shared.
var myClass = function() {
var mySecret = Math.random();
this.name = "Fred";
}
myClass.prototype.sayHello = function() {
return 'Hello my name is ' + this.name;
};
, , . , . : http://www.crockford.com/javascript/private.html