Created
March 5, 2011 23:00
-
-
Save Dykam/856809 to your computer and use it in GitHub Desktop.
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
prop = (obj, props) -> | |
for prop, getset of props | |
if getset.get | |
obj.__defineGetter__ prop, -> getset.get.apply obj, arguments | |
if getset.set | |
obj.__defineSetter__ prop, -> getset.set.apply obj, arguments | |
class SomeCar | |
constructor: -> | |
_doors = 4 | |
_seats = 2 | |
_seatsTaken = 0 | |
prop @, | |
doors: | |
get: -> _doors | |
seats: | |
get: -> _seats | |
seatsTaken: | |
get: -> _seatsTaken | |
set: (val) -> | |
throw new Error "There are not enough seats" if val > _seats | |
_seatsTaken = val | |
car = new SomeCar() | |
car.seatsTaken = 2 # success | |
car.seatsTaken = 4 # error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment