When looking at the code example provided, it is interesting to note that the doOtherStuff
function is defined directly on the b
instance, rather than being higher up in the prototype chain (like on base
or Object
). This leads to a situation where b.hasOwnProperty('doOtherStuff')
returns false. Why might this be the case?
var base = (function () {
var cls = function () { };
cls.prototype.doStuff = function () {
console.log('dostuff');
};
return cls;
})();
var child = (function () {
var cls = function () {
base.call(this);
};
cls.prototype = Object.create(base.prototype);
cls.prototype.constructor = child;
cls.prototype.doOtherStuff = function () { // <--
console.log('doOtherStuff');
}
return cls;
})();
var b = new child();
console.log(b.hasOwnProperty('doOtherStuff'), 'doOtherStuff' in b); //false true