Skip to content

Instantly share code, notes, and snippets.

@eluck
Forked from howlingblast/gist:5814547
Last active August 29, 2015 14:07
Show Gist options
  • Save eluck/f0226f36ac4551d1ebba to your computer and use it in GitHub Desktop.
Save eluck/f0226f36ac4551d1ebba to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment