Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?

前端 未结 2 1826
星月不相逢
星月不相逢 2021-01-14 07:59

I\'m using protypal inheritance and I would like to call an overridden method on the base class. In PHP I could do this using parent::functionName. Is this possible using Ja

2条回答
  •  清酒与你
    2021-01-14 08:14

    With the code defined as I have it in the following fiddle: http://jsfiddle.net/JAAulde/psdym/2/

    The best way is to .call() or .apply() A's method inside of B's:

    //Inside of B.doSomething
    A.prototype.doSomething.call( this, arg1, arg2 );
    

    Or, to simply pass all params that came into B.doSomething() in one fell swoop

    //Inside of B.doSomething
    A.prototype.doSomething.apply( this, arguments );
    

    An amazing book (written by a friend of mine) for learning about various patterns for inheritance in JS is http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X

提交回复
热议问题