Javascript:从父函数(super?)调用子函数。
两个父函数都被子函数覆盖。孩子的两个正在呼叫父母的两个。但是,我希望在父级,对one的调用将调用子级的方法。有没有一个概念是我遗漏的 提前谢谢你Javascript:从父函数(super?)调用子函数。,javascript,inheritance,prototype-programming,Javascript,Inheritance,Prototype Programming,两个父函数都被子函数覆盖。孩子的两个正在呼叫父母的两个。但是,我希望在父级,对one的调用将调用子级的方法。有没有一个概念是我遗漏的 提前谢谢你 函数父函数(){} parent.prototype.one=函数(){ $('body').append(“父项:一个”); } parent.prototype.two=函数(){ 这个; $('body').append(“父项:两个”); } 函数child(){} child.prototype=新父级(); child.prototype
函数父函数(){}
parent.prototype.one=函数(){
$('body').append(“父项:一个
”);
}
parent.prototype.two=函数(){
这个;
$('body').append(“父项:两个
”);
}
函数child(){}
child.prototype=新父级();
child.prototype.constructor=child;
child.prototype.one=function(){//是否不调用此函数?
$('body').append('Child:one
');
}
child.prototype.two=函数(){
$('body').append('Child:在此处执行一些子操作并调用parent:
');
prototype.two();
}
var k=新的子对象();
k、 二();
我知道你想要什么。。。
定义您的功能如下:
child.prototype.two = (function(){
if(child.prototype.two){
var tmp = child.prototype.two;
return function(){
$('body').append('Child: do some child stuff here and call parent: <br />');
tmp.apply(this,arguments);
};
}
})()
child.prototype.two=(函数(){
if(child.prototype.two){
var tmp=child.prototype.two;
返回函数(){
$('body').append('Child:在此处执行一些子操作并调用parent:
');
tmp.apply(此,参数);
};
}
})()
如果原型上没有定义相同的函数,可以添加else条件以返回函数。slebetman回答:
parent.prototype.two.call(this)
而不是直接调用父函数的两个函数 更优化的方法几乎就像您正在做的那样,但是您在
上面调用父方法:
child.prototype.two = function(arg1, arg2) {
parent.prototype.two.call(this, arg1, arg2);
};
但是我建议您使用自定义函数进行扩展,您可以从使用extend
如果您正在使用ECMAScript 5 getter/setter(如果不只是使用第一个getter/setter),您可以在
根据Edward院长的想法,两者都可以以相同的方式使用:
var Animal = extend(Object, {
constructor: function(name) {
// Invoke Object's constructor
this.base();
this.name = name;
// Log creation
console.log('New animal named ' + name);
},
// Abstract
makeSound: function() {
console.log(this.name + ' is going to make a sound :)');
},
});
var Dog = Animal.extend({
constructor: function(name) {
// Invoke Animals's constructor
this.base(name);
// Log creation
console.log('Dog instanciation');
},
bark: function() {
console.log('WOF!!!');
},
makeSound: function() {
this.base();
this.bark();
}
});
var pet = new Dog('buddy');
// New animal named buddy
// Dog instanciation
pet.makeSound();
// buddy is going to make a sound :)
// WOF!!!
在您的情况下,它可以是:
var parent = extend(Object, {
one: function() {
$('body').append("Parent: one <br/>");
},
two: function() {
this.one();
$('body').append("Parent: two <br/>");
}
});
var child = parent.extend({
one: function() {
$('body').append('Child: one <br />');
},
two: function() {
$('body').append('Child: do some child stuff here and call parent: <br />');
this.base();
}
});
var parent=extend(对象{
一:函数(){
$('body').append(“父项:一个
”);
},
二:功能(){
这个;
$('body').append(“父项:两个
”);
}
});
var child=parent.extend({
一:函数(){
$('body').append('Child:one
');
},
二:功能(){
$('body').append('Child:在此处执行一些子操作并调用parent:
');
这个。base();
}
});
this.one()
在您孩子的two()中调用parent.prototype.one()
,因为this
是parent.prototype
有没有办法调用child.prototype.one?只要调用child.prototype.one()
?我不确定你到底想做什么。这一切都是为了什么?但这不违背继承的目的吗?为什么不简单地parent.prototype.two.call(this)
?
var parent = extend(Object, {
one: function() {
$('body').append("Parent: one <br/>");
},
two: function() {
this.one();
$('body').append("Parent: two <br/>");
}
});
var child = parent.extend({
one: function() {
$('body').append('Child: one <br />');
},
two: function() {
$('body').append('Child: do some child stuff here and call parent: <br />');
this.base();
}
});