3

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 JavaScript protypal inheritance?

Consider the following example:

var A = function(){

    this.doSomething = function(){
         console.log('doSomething in A');
    };

};


var B = function() {

   this.doSomething = function(){
         //I would like to call A.doSomething()
         //I tried this.prototype.doSomething() and A.doSomething.call(this), but neither works
         console.log('doSomething in B');
    };

};

B.prototype = new A();


var test = new B();
test.doSomething();

The output that I'm looking for in the console is:
doSomething in B
doSomething in A

Mark Brown
  • 12,026
  • 8
  • 27
  • 32

2 Answers2

2

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

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • Do you mean `B.prototype`? `A.prototype` is empty in this example. – Reid Apr 01 '11 at 01:54
  • Would `doSomething` be available on `A.prototype` if the function was declared *inside* the constructor? – sdleihssirhc Apr 01 '11 at 01:54
  • Ahh, sorry, wan't paying close enough attention to the exact example. My answer is not exactly right, though it would be with a different setup of the original code... – JAAulde Apr 01 '11 at 01:57
  • Hmmm. This only works if doSomething is defined on A's prototype rather than inside of A. – Mark Brown Apr 01 '11 at 02:28
  • Yes, that is correct. It is better to do so anyway unless doSomething() needs access to variables which are scoped inside of function A(). Putting doSomething() inside of A() causes the overhead of redefinition every time A is invoked/constructed. – JAAulde Apr 01 '11 at 02:34
0

from my own best practices, best to create

var parent = ...;

when creating the object if reference to the parent is actually needed - which is not as much as one would expect. then use as desired.

I personally think the term "objects" is misleading in reference to javascript since

object == function == closure

essentially giving the tools for an objectness without limiting to formal object / inheritance constraints. on the up side it makes a very fluid, mutable language; on the downside, you need to fill in yourself missing pieces when needed.

cc young
  • 18,939
  • 31
  • 90
  • 148
  • I know what you're saying, but I don't think it's so much that "objects" is misleading, as JS gets confusing to people who are used to OOP with classical inheritance (such as PHP, though its implementation is flaky). To get classical inheritance in JS requires a whole lot of shoe-horning and usually reveals a misunderstanding of the language and the paradigm in which it functions (<-- pun!). – JAAulde Apr 01 '11 at 11:52