Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling coffescript super methods

Tags:

coffeescript

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

like image 319
pistacchio Avatar asked Jul 17 '12 09:07

pistacchio


1 Answers

I found the answer myself, it should be:

class Dog extends Animal      say: () ->         super         console.log "Hello from dog called #{ @name }" 
like image 156
pistacchio Avatar answered Sep 29 '22 23:09

pistacchio