Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active October 13, 2015 05:17
Show Gist options
  • Save frostney/4144794 to your computer and use it in GitHub Desktop.
Save frostney/4144794 to your computer and use it in GitHub Desktop.
Properties in CoffeeScript classes: Not completely my code, but I found it extremely useful
Function::property = (prop, desc) ->
Object.defineProperty @prototype, prop, desc
Function::staticProperty = (prop, desc) ->
Object.defineProperty @, prop, desc
class MyClass
privateVar = 5
testVar = 10
constructor: ->
# Doing stuff here
# Instance property
@property 'var'
get: -> privateVar
@property 'test'
get: -> testVar
set: (value) -> testVar = value if value > 5
# Static property
@staticProperty 'staticVar'
get: -> privateVar * 20
@frostney
Copy link
Author

frostney commented Mar 7, 2013

This doesn't work in CoffeeScript 1.5.0+ any more.
There are two alternatives:
Use

@property 'test', 
  ...

or

https://gist.github.com/Stoney-FD/5076576

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