gpt4 book ai didi

javascript - 我可以在 Coffeescript 中继承类定义代码吗?

转载 作者:行者123 更新时间:2023-11-30 05:51:01 25 4
gpt4 key购买 nike

基本上,我想做这样的事情:

class Animal
@type: 'animal'
console.log "#{ @type } type defined"

class Dog extends Animal
@type: 'dog'
...???...

当这两个类被加载时,控制台输出看起来像

animal type defined
dog type defined

FWIW,记录第一行;第二个是我遇到问题的地方。

我试过使用 __super__ 和诸如此类的东西,但那些引用了构造函数。我正在尝试获取父类(super class)函数定义本身...

最佳答案

由于您希望在类加载(函数定义)上显示日志消息,因此您不能使用继承(通过调用 super 构造函数或其他方式)。随便写

class Animal
@type: 'animal'
console.log "#{ @type } type defined"

class Dog extends Animal
@type: 'dog'
console.log "#{ @type } type defined"

关于javascript - 我可以在 Coffeescript 中继承类定义代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com