Skip to content

Instantly share code, notes, and snippets.

@melvynhills
Created May 22, 2012 10:13
Show Gist options
  • Save melvynhills/2768142 to your computer and use it in GitHub Desktop.
Save melvynhills/2768142 to your computer and use it in GitHub Desktop.
Getter/setter and private variables in CoffeeScript
class Foo
_myPrivateVar = "foo bar"
@:: __defineGetter__ "myVar", ->
_myPrivateVar
@:: __defineSetter__ "myVar", (value) ->
_myPrivateVar = value
foo = new Foo
console.log foo._myPrivateVar # undefined
console.log foo.myVar # foo bar
foo.myVar = "baz qux"
console.log foo.myVar # baz qux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment