Last active
November 13, 2017 14:12
-
-
Save aderaaij/c1e55fc80ec0a9056ba57945c2592505 to your computer and use it in GitHub Desktop.
A few simple examples of destructuring objects. From es6.io
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
const person = { | |
first: 'Wes', | |
last: 'Bos', | |
country: 'Canada', | |
city: 'Hamilton', | |
twitter: '@wesbos', | |
}; | |
// Destructure an object as values with the keyname | |
const { first, last, twitter } = person; | |
const arden = { | |
first: 'Arden', | |
last: 'de Raaij', | |
links: { | |
social: { | |
twitter: 'https://twitter.com/ardennl', | |
instagram: 'https://instagram.com/ardennl', | |
}, | |
web: { | |
blog: 'https://arden.nl' | |
} | |
} | |
}; | |
// Rename value names while destructuring | |
const { twitter: tweet, instagram: ig } = arden.links.social; | |
// Example of a settings object; a couple of default values | |
const settings = { width: 300, color: 'black' } // height, fontSize | |
// Adding default values while destructering an object. In this case width and color | |
// will return what is defined in the settings object, height and fontsize values are | |
// defined while destructuring | |
const { width = 100, height = 100, color = 'blue', fontSize = 25} = settings; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment