javascript - CoffeeScript - How do I retrieve a static array property in class -
i started learning coffeescript , i'd know best practice retrieving static property in class child instance.
class mutant mutantarray: [] constructor: (@name, @strength = 1, @agility = 1) -> @mutantarray.push(@name) attack: (opponent) -> if opponent in @mutantarray console.log @name + " attacking " + opponent else console.log "no mutant name of '" + opponent + "' found." @getmutants: () -> # right? console.log @.prototype.mutantarray wolverine = new mutant("wolverine", 1, 2) rogue = new mutant("rogue", 5, 6) rogue.attack("wolverine") mutant.getmutants()
i getmutants() method static (no instantiation needed) , return list of mutant names have been instantiated. @.prototype.mutantarray seems work fine, there better way this? tried @mutantarray doesn't work.
thanks!
i think should define mutantarray static field. then, non-static methods should reference via class , static methods access via @. this:
class mutant @mutantarray: [] constructor: (@name, @strength = 1, @agility = 1) -> mutant.mutantarray.push(@name) attack: (opponent) -> if opponent in mutant.mutantarray console.log @name + " attacking " + opponent else console.log "no mutant name of '" + opponent + "' found." @getmutants: () -> # right? console.log @mutantarray
Comments
Post a Comment