在这段JavaScript代码中,当实例化
new child
导致其构造函数执行时,create方法似乎没有创建父对象。子对象似乎没有继承父对象的成员函数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!!!