Skip to content

Instantly share code, notes, and snippets.

@remino
Last active August 29, 2015 14:26
Show Gist options
  • Save remino/ed72681f3b3510b17f89 to your computer and use it in GitHub Desktop.
Save remino/ed72681f3b3510b17f89 to your computer and use it in GitHub Desktop.
Helpers for JavaScript to easily define getters and setters in CoffeeScript
Function::getter = (prop, get) ->
Object.defineProperty @prototype, prop, {get, configurable: yes}
Function::setter = (prop, set) ->
Object.defineProperty @prototype, prop, {set, configurable: yes}
@remino
Copy link
Author

remino commented Aug 6, 2015

Example:

class XY
  @getter 'square', -> x * y
  constructor: (x, y) ->

class DocumentLocator
  @getter 'location', -> self.document.location
  @setter 'location', (address) -> self.document.location = address

xy = new XY(4, 4)
xy.square # 16

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