调用coffescript超级方法 [英] Calling coffescript super methods
本文介绍了调用coffescript超级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
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()
b $ b
结果不是
The result is not
Hello from animal called Bobby
Hello from animal called Duffy
Hello from dog called Duffy
但我得到以下错误:
Hello from animal called Bobby
Hello from animal called Duffy
Uncaught TypeError: Cannot call method 'say' of undefined
super是如何定义的?如何调用父方法以扩展它?感谢
How come super is undefined? How to call a parent method in order to extend it? Thanks
推荐答案
我自己找到答案,应该是:
I found the answer myself, it should be:
class Dog extends Animal
say: () ->
super
console.log "Hello from dog called #{ @name }"
这篇关于调用coffescript超级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文