var Person = function(name){
this.name = name;
this.sayName = function () {
console.log("My Name is "+ this.name);
}
}
var nitin = new Person("Nitin");
nitin.sayName(); // My Name is Nitin
// Codebreak
var Person = function(name){
this.name = name;
}
Person.prototype.sayName = function (){
console.log("My Name is "+ this.name);
}
var nitin = new Person("Nitin");
nitin.sayName(); // My Name is Nitin
I was going though inheritance in JS, but for both the approaches above result is same. So I am confused here which approach to follow and why?