Could someone please explain the differences of the two code below using a constructor function. They both give the same results. Does one have an advantage over the other?
function Person(){
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
}
var person1 = new Person();
var person2 = new Person();
person1.name = "Greg";
alert(person1.name); //"Greg" from instance
alert(person2.name); //"Nicholas" from prototype
VERSUS
function Person(){
this.name = "Nicholas";
this.age = 29;
}
var person1 = new Person();
var person2 = new Person();
person1.name = "Greg";
alert(person1.name); // "Greg" from instance
alert(person2.name); // "Nicholas" from Person Object?