Search code examples
coffeescriptsuperclasssuper

Call an overwritten method in coffeescript


I want to call a method (foo) in Coffee-script from a subclass. I know I can do this with @foo if I didn't overwrite foo in the subclass, or with super if I did overwrite it and I'm calling from the subclass' foo method.

However, I would like to call superclass' foo method from subclass bar method. How can this be done?


Solution

  • Not sure why you need this but ...

    class A
      foo: ->
        console.log 'A'
    
    class B extends A
      foo: ->
        console.log 'B'
      bar: ->
       A::foo.call @
    
    new B().bar()