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
?
Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference.
Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference. VariableName.
An instance variable is just a property of an object, as Felix Kling said. You can't use props because that's referencing a global or local variable called props . What you want to access is the current value of props for the current component, stored in this.
You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With