Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =======================================================================
- # INCORRECT
- # =======================================================================
- class Foo
- # don't expect this to be an instance variable. it is not!! it is the
- # java equivalent of: public static colours = new Array()
- colours: []
- addColour: (colour) =>
- # i believe java wouldn't even compile this with this.colours, but
- # Javascript is such a whore sometimes, it lets it pass without
- # complaining
- @colours.push colour
- bar = new Foo()
- bar.addColour "red"
- console.log bar.colours
- # [ "red" ]
- bat = new Foo()
- bat.addColour "orange"
- console.log bat.colours
- # [ "red", "orange" ]
- # ^^^ ack!
- # =======================================================================
- # CORRECT
- # =======================================================================
- class Foo
- constructor: =>
- # initialize instance variables in your object's constructor
- @colours = []
- addColour: (colour) =>
- @colours.push colour
- bar = new Foo()
- bar.addColour "red"
- console.log bar.colours
- # [ "red" ]
- bat = new Foo()
- bat.addColour "orange"
- console.log bat.colours
- # [ "orange" ]
Add Comment
Please, Sign In to add comment