1. 程式人生 > >ECMAScript 6知識點總結 --- 解構賦值

ECMAScript 6知識點總結 --- 解構賦值

  • 陣列
    let [a, b, c] = [1, 2, 3]
    
    let [a, [b, c]] = [1, [2, 3]]

    注意:左右兩邊結構格式要保持一致

  • json

    let json = {name: 'cc', age: 18, job: 'coder'}
    let {name, age, job} = json
    console.log(name, age, job)
    // 'cc', 18, 'coder'
    let json = {name: 'cc', age: 18, job: 'coder'}
    let {name:n, age:a, job:j} = json
    console.log(n, a, j)
    // 'cc', 18, 'coder'

    解構時也可以給預設值

    let [a, b, c='預設值'] = [1, 2]
    console.log(a, b, c)
    // 1, 2, '預設值'