I'm in doubt about creating instance variables in a class / JavaScript constructor function. I have read in several places that the declaration of an instance variable is made inside the body of the class as in the example below:
function Spam() {
this.foo = "foo"
}
But I realized that I can also declare the variable in prototype
of the class as in the example below:
Spam.prototype.bar = "bar";
And in the end I get the same result:
var mySpam = new Spam();
mySpam.foo // => "foo": declarado no corpo da classe
mySpam.bar // => "bar": declarado no prototype da classe
What is the difference between the two methods of declaring an instance variable?