【发布时间】:2013-11-18 15:21:44
【问题描述】:
想象以下场景:
我有两个班级:Parent 和 Child。 Parent 有一个方法foo()。 Child 想要覆盖 foo(),并在其中执行 foo() 在 Parent 的 foo() 中所做的任何事情。
在任何其他编程语言中,我都会做类似的事情
foo(){
super.foo();
//do new stuff
}
但在 javascript 中没有这样的东西。这是我的代码的简短版本:
function Parent( name, stuff ){
this.name = name;
this.stuff = stuff;
}
Parent.prototype = {
foo: function(){
console.log('foo');
}
}
function Child(name, stuff, otherStuff ){
Parent.call(this, name, stuff);
this.otherStuff = otherStuff;
}
Child.prototype = new Parent();
Child.prototype.foo = function(){
???//I want to call my parents foo()! :(
console.log('bar');
}
我想要实现的是,当Child 的实例调用foo() 时,我可以在控制台中获得foobar。
谢谢!
PS:请不要使用 JQuery、PrototypeJS、ExtJs 等……这是一个 Javascript 项目,也是一个学习练习。谢谢。
【问题讨论】:
标签: javascript