I have a Person constructor function similar to sayHello method
var Person = function(firstName,lastName) { this.lastName = lastName; this.sayHello = function() { return "Hi there " + firstName; } };
Then I define a different version of the sayHello method on the Person prototype:
Object.defineProperties(Person.prototype,{ sayHello: { value: function() { return 'Hi there'; }, enumerable: true } });
Now, if I create an instance of Person and call sayHello on it, I notice that it uses the version of sayHello that is defined on the prototype.
var JoeBlow = new Person('Joe','Blow'); > JoeBlow.sayHello() < "Hi there"
It bothers me.
Why doesn't the JoeBlow object use its own sayHello implementation, instead of looking for sayHello on its prototype?
source share