Created
January 10, 2016 16:57
-
-
Save alexnm/91d58cff0a7aa1c4ee55 to your computer and use it in GitHub Desktop.
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
// A factory method which returns an object | |
const factory = ( ) => { a: 1, b: 2 } | |
const { a, b } = factory( ); // a = 1, b = 2 | |
// A method that takes a Person object as a parameter and returns the full name | |
// firstName and lastName get destructured into variables inside the function | |
const fullName = ( { firstName, lastName } ) => firstName + ' ' + lastName; | |
const person = { | |
firstName: 'Alex', | |
lastName: 'M', | |
sex: 'male', | |
age: 27 | |
}; | |
console.log( fullName( person ) ); | |
// Destructuring with arrays is fun too | |
const array = [ 1, 2, 3, 4, 5 ]; | |
let [ a, b ] = [ 1, 2 ]; // a = 1, b = 2 | |
let [ a ] = array; // a = 1, first element of the array | |
let [ a, , b ] = array; // a = 1, b = 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the difference between "const factory = ( ) => { a: 1, b: 2 }" and "const factory = {a:1, b:2}"? I understand the former defines a function, and the latter is a struct. But what's the benefit of making factory a function here?
By the way, I couldn't get the former to work on JSFiddle.