-
-
Save mralexgray/eac81b64f30d19ac2b0086fabe286f9b to your computer and use it in GitHub Desktop.
CoffeeScript: getter/setter example
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
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