Skip to content

Instantly share code, notes, and snippets.

@mralexgray
Forked from howlingblast/gist:5814547
Created November 10, 2019 00:14
Show Gist options
  • Save mralexgray/eac81b64f30d19ac2b0086fabe286f9b to your computer and use it in GitHub Desktop.
Save mralexgray/eac81b64f30d19ac2b0086fabe286f9b 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment