Đưa ra một ví dụ về destructuring một object hoặc một array trong ES6?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
217 lượt xem
Javascript senior

Destructuring là một biểu thức có sẵn trong ES6 cho phép một cách ngắn gọn và thuận tiện để trích xuất các giá trị của Object hoặc Array và đặt chúng vào các biến riêng biệt.

Array destructuring

// Variable assignment.
const foo = ['one', 'two', 'three'];
const [one, two, three] = foo;
console.log(one); // "one"
console.log(two); // "two"
console.log(three); // "three"
// Swapping variables
let a = 1;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

Object destructuring

// Variable assignment.
const o = { p: 42, q: true };
const { p, q } = o;
console.log(p); // 42
console.log(q); // true
Câu hỏi tiếp theo: Giải thích Prototype Design Pattern?
{{login.error}}