Javascript:从父(超级?)函数调用子函数。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19019287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Calling child function from parent (super?) function.
提问by alotofquestions
Both parent functions are overridden by child. two in the child is calling parent's two. however, i was expecting that at the parent level, the call to one would invoke the child's method. Is there a concept that i am missing?
两个父函数都被子函数覆盖。孩子中的两个正在调用父母的两个。然而,我期望在父级,对一个的调用会调用子级的方法。有没有我缺少的概念?
Thank you in advance!
先感谢您!
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();
采纳答案by A. Matías Quezada
The more optimal way is almost like you are doing it, but you call the parent method over this
:
更优的方法几乎就像你在做一样,但你调用父方法this
:
child.prototype.two = function(arg1, arg2) {
parent.prototype.two.call(this, arg1, arg2);
};
But I recommend you to use a custom function to extend, you can use extend
from jsbase
但是我建议你使用自定义函数来扩展,你可以extend
从jsbase使用
If you are using ECMAScript 5 getters/setters (if not just use the first one) you may prefeer to use the one at this gist
如果您正在使用 ECMAScript 5 的 getter/setter(如果不只是使用第一个),您可能更喜欢使用本要点中的那个
Both can be used the same way based on Dean Edward's idea:
两者都可以根据 Dean 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!!!
In your case it can be:
在您的情况下,它可以是:
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();
}
});
回答by grape_mao
Well, I understand what you want... define you function like this:
好吧,我明白你想要什么......定义你的功能是这样的:
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);
};
}
})()
You could add else condition to return a function if there is no same function defined on prototype.
如果原型上没有定义相同的函数,您可以添加 else 条件以返回函数。
回答by alotofquestions
Answered by slebetman:
slebetman 的回答:
parent.prototype.two.call(this)
parent.prototype.two.call(this)
Instead of directly invoking the parent's two function.
而不是直接调用父的两个函数。