gpt4 book ai didi

coffeescript - 从 CoffeeScript 中的类实例获取类变量

转载 作者:行者123 更新时间:2023-12-03 20:11:02 25 4
gpt4 key购买 nike

我有一个这样的类(class):

class Cow
@feet : 4

constructor: (@name) ->

bes = new Cow "Bessie"

问题是,是否可以仅在给定 bes 的情况下才能访问脚?

最佳答案

您可以使用 JavaScript constructor property去上课,在那里你会找到你的 feet :

class Cow
@feet: 4
constructor: (@name) ->

class HexaCow extends Cow
@feet: 6

bes = new Cow('Bessie')
pan = new HexaCow('Pancakes')

alert(bes.constructor.feet) # 4
alert(pan.constructor.feet) # 6


演示: http://jsfiddle.net/ambiguous/ZfsqP/

我不知道 constructor 有什么特殊的 CoffeeScript 替代品尽管。

关于coffeescript - 从 CoffeeScript 中的类实例获取类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525740/

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