Skip to content

Instantly share code, notes, and snippets.

@alexaivars
Created January 12, 2012 08:41
Show Gist options
  • Save alexaivars/1599437 to your computer and use it in GitHub Desktop.
Save alexaivars/1599437 to your computer and use it in GitHub Desktop.
Getter Setter patter for Coffeescript
Function::define = (prop, desc) ->
Object.defineProperty this.prototype, prop, desc
class GetterSetterTest
constructor: (@_obj = {}) ->
# 'obj' is defined via the prototype, the definition proxied through
# to 'Object.defineProperty' via a function called 'define' providing
# some nice syntactic sugar. Remember, the value of '@' is
# GetterSetterTest itself when used in the body of it's class definition.
@define 'obj'
get: ->
return @_obj
set: (value) ->
@_obj = value
_obj: null
@alexaivars
Copy link
Author

Thanks to https://github.com/JHawkley for the code.

@dr-dimitru
Copy link

@alexaivars

@define 'obj', # Missed comma
        get: ->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment