3

i tried to find a way to call overridden method of superclass in JS and got this.

function A() {
    this.superclass = new Array(A.prototype);
}
A.prototype.call_method = function(method, args, pos) {
    if (!(pos >= 0)) pos = this.superclass.length - 1;
    while (!this.superclass[pos].hasOwnProperty(method)) if ((--pos) < 0) return;
    if (this.superclass[pos][method]) this.superclass[pos][method].apply(this, args);
    if (pos) this.call_method(method, args, pos - 1);
}
A.prototype.add = function() {
    this.superclass.push(arguments.callee.caller.prototype);
}
A.prototype.test = function(a, b, c) {
    alert("test of A ");
}

function B() {
    A.call(this);
    this.add();
}
B.prototype = new A();
B.prototype.constructor = B;
B.prototype.test1 = function(a, b, c) {
    alert("test of B ");
}

function C() {
    B.call(this);
    this.add();
}
C.prototype = new B();
C.prototype.constructor = C;
C.prototype.test = function(a, b, c) {
    alert("test of C");
}

var aa = new C();
aa.call_method("test", [1, 2, 3]);

What do You think, is it ok ? Or maybe it can produce 'memory leaks' (reference to own prototype)? Thanks a lot


Thank You for your reply, i tried Your code out . But if I subclass SubClass, for examle var a= SubClass1(), ( SubClass1 has its own foo())

and call a.foo(), then only SubClass1 and BaseClass foo's will be called,

not SubClass foo() Code:

function BaseClass() { } BaseClass.prototype.foo = function() {     
  alert('called BaseClass.foo'); 
};  
SubClass = function() { }; 
SubClass.prototype = new BaseClass; 
SubClass.prototype.foo = function() {     
    alert('called SubClass.foo');      
    // calling super.foo();   
    this.constructor.prototype.foo.call(this); 
}; 
SubClass1 = function() { }; 
SubClass1.prototype = new SubClass; 
SubClass1.prototype.foo = function() {     
    alert('called SubClass1.foo');      
    // calling super.foo();   
    this.constructor.prototype.foo.call(this); 
}; 
var a=new SubClass1();
a.foo();

Thanks

2
  • Could you explain your code in short? Commented Sep 4, 2011 at 16:27
  • I'm not sure what you want, but maybe Object.getPrototypeOf can help you out... Commented Sep 4, 2011 at 16:30

2 Answers 2

7

You can use a property to store the class' parent and call methods directly on the parent class:

function BaseClass() {
}
BaseClass.prototype.foo = function() {
    console.log('called BaseClass.foo');
};

SubClass = function() {
};
SubClass.prototype = new BaseClass;
SubClass.prototype.__super__ = BaseClass;
SubClass.prototype.foo = function() {
    console.log('called SubClass.foo');

    // calling super.foo();
    this.__super__.prototype.foo.call(this);
};

You can add a little abstraction:

// The `clazz` parameter allows super-classes to use super() too.
// It should be the calling class
BaseClass.prototype.super = function(clazz, functionName) {
    // take all the arguments after functionName
    var args = Array.prototype.slice.call(arguments, 2);
    // call the function on super's prototype
    clazz.prototype.__super__.prototype[functionName].apply(this, args);
};

You can use it like this:

SubClass.prototype.foo = function() {
    console.log('called SubClass.foo');

    // calling super.foo();
    // Pass the calling class as first parameter
    this.super(SubClass, 'foo');
};    

Try it here: http://jsfiddle.net/ZWZP6/2/

In CoffeeScript

In coffee script you can use the super()[docs] construct:

class BaseClass
    foo: -> console.log('called BaseClass.foo')

class SubClass extends BaseClass
    foo: ->
        console.log('called SubClass.foo')
        super()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You all , but my problem is to get 3 or more clases(subclasses), so that they all have a method with the same name. A()->B()->C(), all of them have method foo(), and if i call C.foo(), then B.foo() and A.foo() should be called too. my Code works, but maybe it is really bad... that was the question
I've updated the answer and the example code at jsfiddle.net/ZWZP6/2 ; this works with more than one level of inheritance
I think you've just sold me on CoffeeScript
1

In 2015 version 6 of ECMAScript was released. One of the new features was native classes.

The parent class is accessible via the new super keyword.

Your example of a sub-class of a sub-class of a class could be written as:

class A {
  foo() {
    console.log('A');
  }
}

class B extends A {
  foo() {
    super.foo();
    console.log('B');
  }
}

class C extends B {
  foo() {
    super.foo();
    console.log('C');
  }
}

const o = new C();
o.foo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.