All Articles

Some javascript

  1. Destructuring Assignment (ES6)

    const customer = {  
    age: 25,
    name: "Peter"
    }
    const age = customer.age
    const name = customer.name
    console.log(age)    /* 25 */
    console.log(name)   /* Peter */

Có thể viết lại theo cách khác

const age = 40;
const customer = {  
    age: 25,
    name: "Peter"
}

const {age: customerAge, name} = customer

console.log(age)           /* 40 */
console.log(customerAge)   /* 25 */
console.log(name)          /* Peter */
  1. Rest parameter vs spread syntax

Bên Java thì gọi là varargs (variable arguments)

function sum(...theArgs) {
  let total = 0;
  for (const arg of theArgs) {
    total += arg;
  }
  return total;
}

console.log(sum(1, 2, 3));
// expected output: 6

console.log(sum(1, 2, 3, 4));
// expected output: 10
  1. Template literals
const 

Ref:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax