Skip to content

Instantly share code, notes, and snippets.

@conanak99

conanak99/destructuring.js Secret

Last active Mar 8, 2016
Embed
What would you like to do?
// Với array
var foo = ["one", "two", "three"];
// Cách cũ
var one = foo[0];
var two = foo[1];
var three = foo[2];
// Dùng destructuring
var [one, two, three] = foo;
// Với object
var obj = {firstName:'Hoang', lastName:'Pham'};
// Cách cũ
var firstName = obj.firstName;
var lastName = obj.lastName;
// Dùng destructuring
var {firstName, lastName} = obj;
// Nếu muốn lấy tên biến khác tên field của object
var {firstName : fn, lastName : ln} = obj; //fn: Hoang, ln: Pham
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment