Let me put first two examples.
Example 1:
function Gallery() {
this.a = "I am 'A'";
this.trace = function() {
console.log(this.a);
}
}
Example 2:
function Gallery() {
this.a = "I am 'A'";
}
Gallery.prototype.trace = function () {
console.log(this.a);
}
Obviously, both things do the same. My question is: is there any drawback to using the method definition in the constructor function over the prototype one? What exactly is the difference anyway?
Is the prototype more memory friendly? I assume that if I define the method inside the constructor function, every single Gallery instance will have its 'own instance' of the method so will consume more memory while the prototype defines only one function that's shared across all the Gallery instances. Is this correct?
Thanks a lot!