This is because of prototype chain. JavaScript runtime will look for the nearest definition of requested property.
Since you're defining showName in person during the constructor call, you're defining a showName in the object rather than in the prototype. Runtime will look for the nearest property called showName and it'll find this.showName before prototype.showName.
Just try to define showName in person in the prototype instead of in the object itself:
function person() {
...
}
person.prototype = {
showName: function() {
...
}
}
...and you'll get the expected result!
In other words...
Let's say you've the following JavaScript constructor function:
var A = function() {
this.doX = function() {};
};
A.prototype = {
doX: function() { }
};
What function will be called if I create an instance of A?
var instance = new A();
instance.doX(); // ?????
The doX() added during the construction time will be the one called by the runtime instead of the one defined in the prototype.
In fact, the doX() defined in the A constructor is equivalent to just doing the following declaration:
var instance = new A();
instance.doX = function() {};
The this keyword inside the A constructor is a reference of the instantiated object, thus it's the same declaring it inside or outside the constructor - actually there's a single difference: if you declare outside the constructor, not all A instances will own the whole property... -.
At the end of the day, properties declared in the object hide ones declared in the prototype, and this is what happened in your code, because employee calls the person constructor with the this reference of employee, meaning that showName in person will hide the one declared in the employee prototype.