Skip to content

Instantly share code, notes, and snippets.

@alexnm
Created January 10, 2016 16:57
Show Gist options
  • Save alexnm/91d58cff0a7aa1c4ee55 to your computer and use it in GitHub Desktop.
Save alexnm/91d58cff0a7aa1c4ee55 to your computer and use it in GitHub Desktop.
// 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
@tellyipock
Copy link

tellyipock commented Jul 27, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment