【发布时间】:2014-08-18 05:48:45
【问题描述】:
我刚开始学习 CoffeeScript,我想知道从子实例中检索类中的静态属性的最佳做法是什么。
class Mutant
MutantArray: []
constructor: (@name, @strength = 1, @agility = 1) ->
@MutantArray.push(@name)
attack: (opponent) ->
if opponent in @MutantArray then console.log @name + " is attacking " + opponent else console.log "No Mutant by the name of '" + opponent + "' found."
@getMutants: () ->
# IS THIS RIGHT?
console.log @.prototype.MutantArray
Wolverine = new Mutant("Wolverine", 1, 2)
Rogue = new Mutant("Rogue", 5, 6)
Rogue.attack("Wolverine")
Mutant.getMutants()
我希望我的 getMutants() 方法是静态的(不需要实例化)并返回已实例化的 Mutant 名称列表。 @.prototype.MutantArray 似乎工作正常,但有没有更好的方法来做到这一点?我尝试了@MutantArray,但这不起作用。
谢谢!
【问题讨论】:
标签: javascript inheritance coffeescript