Last active
December 14, 2015 11:08
-
-
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
This file contains hidden or 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) -> | |
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