Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active December 14, 2015 11:08
Show Gist options
  • Save frostney/5076576 to your computer and use it in GitHub Desktop.
Save frostney/5076576 to your computer and use it in GitHub Desktop.
Different approach to properties in CoffeeScript class (CoffeeScript 1.5.0 compatible) Property names are not written as strings
Function::property = (prop) ->
for key, value of prop
Object.defineProperty @prototype, key, value
Function::staticProperty = (prop) ->
for key, value of prop
Object.defineProperty @, key, value
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
myClass = new MyClass()
console.log myClass.var
console.log myClass.test
myClass.test = 10
console.log myClass.test
console.log myClass.staticVar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment