It's a funny thing: a JS constructor can return an object that becomes this. This object however doesn't follow the prototypes, as defined for the constructor (in this case it's a plain Object
). The correct way that looks like your code would be:
var Human = function() {
var hi = function() {
alert('hi');
};
this.name = "dan";
this.sayHi = hi;
};
// or even:
var Human = function() {
this.name = "dan";
};
Human.prototype.sayHi = function() {
alert('hi');
};
Similar for Coder
. The inheritance code is OK.