Question

I have to implement following code in Javascript, but cannot find equivalent of super.say() in javascript. How to do this translation ?

class Foo {
    public void say() {
      System.out.println("foo");  
    }
}


class Bar extends Foo {

    static int counter = 0;

    public void say() {
        super.say();
        counter++;
        System.out.println("bar " + counter); 
    }
}
Was it helpful?

Solution

There's an article that describes it in detail: http://joshgertzen.com/object-oriented-super-class-method-calling-with-javascript/

Basically, you need to store a reference to your base method, then call it in the "derived" (perhaps "shadowed" might be a better word here) method.

OTHER TIPS

The keyword super doesn't work in Javascript as in Java because there's no notion of classes. Without a class, you can't have a descendant, and thus super is useless in that context.

You could implement something super-like, as described here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top