Skip to content

Instantly share code, notes, and snippets.

@howlingblast
Created June 19, 2013 14:00
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save howlingblast/5814547 to your computer and use it in GitHub Desktop.
Save howlingblast/5814547 to your computer and use it in GitHub Desktop.
CoffeeScript: getter/setter example
Function::property = (prop, desc) ->
Object.defineProperty @prototype, prop, desc
class Person
constructor: (@firstName, @lastName) ->
@property 'fullName',
get: -> "#{@firstName} #{@lastName}"
set: (name) -> [@firstName, @lastName] = name.split ' '
p = new Person 'Leroy', 'Jenkins'
console.log p.fullName # Leroy Jenkins
p.fullName = 'Leroy Monkey'
console.log p.lastName # Monkey
console.log p.fullName # Leroy Monkey
p.lastName = 'Jenkins'
console.log p.fullName
@remoteportal
Copy link

THIS IS SO COOL!

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