pistacchio
pistacchio

Reputation: 58903

Calling coffescript super methods

I have the following code:

    class Animal
        constructor: (@name) -> 
        say: () -> console.log "Hello from animal called #{ @name }"

    class Dog extends Animal

        say: () ->
            super.say()
            console.log "Hello from dog called #{ @name }"

    a = new Animal('Bobby')
    a.say()

    d = new Dog("Duffy")
    d.say()            

The result is not

Hello from animal called Bobby
Hello from animal called Duffy
Hello from dog called Duffy

But I get the following error:

Hello from animal called Bobby
Hello from animal called Duffy
Uncaught TypeError: Cannot call method 'say' of undefined

How come super is undefined? How to call a parent method in order to extend it? Thanks

Upvotes: 34

Views: 14251

Answers (1)

pistacchio
pistacchio

Reputation: 58903

I found the answer myself, it should be:

class Dog extends Animal

    say: () ->
        super
        console.log "Hello from dog called #{ @name }"

Upvotes: 69

Related Questions