Last active
October 13, 2015 05:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work in CoffeeScript 1.5.0+ any more.
There are two alternatives:
Use
or
https://gist.github.com/Stoney-FD/5076576