In this JavaScript code, when the instantiation of new child
causes its constructor to execute, the create method does not seem to creating the parent object. The child does not seem to inherit the parent's member function m
.
function parent() {
parent.prototype.a =2;
parent.prototype.m = function() {
return this.a++;
}
}
function child() {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = parent;
parent.call(this);
}
var c = new child();
alert(child.prototype.m()); // 2
alert(child.prototype.m()); // 3
alert(c.m()); // FAILS!!!