15

I have a class like this:

class Cow
  @feet : 4

  constructor: (@name) ->

bes = new Cow "Bessie"

The question is, is it possible to access feet only given bes?

1 Answer 1

26

You can use the JavaScript constructor property to get at the class and there you will find your 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
​

Demo: http://jsfiddle.net/ambiguous/ZfsqP/

I don't know of any special CoffeeScript replacement for constructor though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.