Created
May 30, 2015 19:01
Simple examples of destructuring in ES6
This file contains hidden or 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
// http://babeljs.io/ | |
// http://shouldjs.github.io/ | |
import * as should from 'should'; | |
let bilbo = { | |
name: 'Bilbo', | |
age: 50, | |
family: ['Baggins', 'Took'], | |
pastTime: ['writing']}; | |
let {name, family:parents} = bilbo; | |
name.should.equal('Bilbo'); | |
parents.should.eql(['Baggins', 'Took']); | |
let hobbies = ({name, pastTime:hobbies}) => name + ' enjoys ' + hobbies.join(', '); | |
hobbies(bilbo).should.equal('Bilbo enjoys writing'); | |
hobbies({ | |
name: 'Gollum', | |
something: 'else', | |
pastTime: ['precious!']}).should.equal('Gollum enjoys precious!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also my post: http://comp-phil.blogspot.com/2015/05/destructuring-in-clojure-and-es6.html and slides: http://www.slideshare.net/MikeHarris10/there-and-back-again-47929525 which go with this gist.