【发布时间】:2013-10-01 21:26:12
【问题描述】:
两个父函数都被子函数覆盖。孩子的二号叫父母的二号。但是,我期望在父级调用 one 会调用子级的方法。有没有我遗漏的概念?
提前感谢您!
function parent(){}
parent.prototype.one = function(){
$('body').append("Parent: one <br/>");
}
parent.prototype.two = function(){
this.one();
$('body').append("Parent: two <br/>");
}
function child(){}
child.prototype = new parent();
child.prototype.constructor = child;
child.prototype.one = function(){ //should this function not be called?
$('body').append('Child: one <br />');
}
child.prototype.two = function(){
$('body').append('Child: do some child stuff here and call parent: <br />');
parent.prototype.two();
}
var k = new child();
k.two();
【问题讨论】:
-
this.one()在您孩子的two()中调用parent.prototype.one(),因为this是parent.prototype -
有没有办法调用child.prototype.one?
-
只需致电
child.prototype.one()吗?我不确定你实际上想要做什么。这一切是为了什么? -
但这不违背继承的目的吗?
-
为什么不简单地
parent.prototype.two.call(this)?
标签: javascript inheritance prototype-programming