简体   繁体   English

javascript对象可以覆盖其父方法并在该方法中调用它吗?

[英]Can a javascript object override its parent method and call it in that method?

Imagine the following scenario: 想象一下以下场景:

I have two classes: Parent and Child . 我有两个班: ParentChild Parent has a method foo() . Parent有一个方法foo() Child wants to override foo() , and within do whatever foo() did in Parent 's foo() . Child想要覆盖foo() ,并在Parentfoo()foo()

In any other programming language i would just do something like 在任何其他编程语言中,我会做类似的事情

foo(){
  super.foo();
  //do new stuff
}

But in javascript there's no such thing. 但是在javascript中没有这样的东西。 Here's a short version of my code: 这是我的代码的简短版本:

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');

}

What I want to achieve is that when an instance of Child calls foo() i can get foobar in the console. 我想要实现的是当一个Child的实例调用foo()我可以在控制台中获得foobar

Thanks! 谢谢!

PS: please, no JQuery, PrototypeJS, ExtJs, etc... This is a Javascript project and also a learning exercise. PS:拜托,没有JQuery,PrototypeJS,ExtJs等......这是一个Javascript项目,也是一个学习练习。 Thank you. 谢谢。

Its simply, you can use the prototype and use call/apply to call the parents function. 简单来说,你可以使用原型并使用call / apply来调用parent函数。

Child.prototype.foo = function(){
  Parent.prototype.foo.apply(this, arguments);
  console.log('bar');
}

Take a look: http://jsfiddle.net/J4wHW/ 看看: http//jsfiddle.net/J4wHW/

First of all, your implementation of inheritance is not great. 首先,你的继承实现并不是很好。 I propose the following change: 我建议进行以下更改:

// Child.prototype = new Parent(); // bad because you instantiate parent here
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

With that in mind, i wrote this helper function: 考虑到这一点,我写了这个辅助函数:

function base(object, methodName) {
  var proto = object.constructor.prototype;
  var args = Array.prototype.slice.call(arguments, 2);
  while (proto = Object.getPrototypeOf(proto)) 
    if (proto[methodName])  
      return proto[methodName].apply(this,args);
  throw Error('No method with name ' + methodName + ' found in prototype chain');
}

// usage:

Child.prototype.foo = function(){
  base(this, 'foo', 'argument1', 'argument2');  
  console.log('bar');
};

It has slightly more than what you wanted, in that you don't have to wonder where the method is defined in the inheritance chain, it will go all the way up to the root and try to find the method. 它比你想要的略多,因为你不必怀疑在继承链中定义方法的位置,它将一直到根并尝试找到方法。 I also expanded your example a bit with a Grandparent to exhibit this issue. 我还用祖父母稍微扩展了你的例子以展示这个问题。 The foo method has been moved from the Parent to the Grandparent (and Parent inherits from Grandparent). foo方法已从Parent移动到祖父母(并且Parent继承自祖父母)。

Grandparent demo: http://jsbin.com/iwaWaRe/2/edit 祖父母演示: http//jsbin.com/iwaWaRe/2/edit

NOTE: The implementation is loosely based on Google Closure Library's implementation of goog.base . 注意:实现基于Google Closure Library的goog.base实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM