首页 > 技术文章 > es6 解构赋值

hekeying 2020-12-15 21:55 原文

// 数组的解构赋值
// let [a, b, [c, d]] = [1, 22, [323, 123123]]
// console.log(a, b, c, d)

// let [a, b, [c]] = [1, 22, [323, 123123]]
// console.log(a, b, c)

// let [a, b, c] = [1, 22, [323, 123123]]
// console.log(a, b, c)

// let [a, b, c, d = 5] = [1, 22, [323, 123123]]
// console.log(a, b, c, d)
// 数组的解构赋值是惰性的 有值取右边 否则去定义的值

//对象的解构赋值
// let user = {
// name: 'hekeying',
// age: 28
// }
// //原本写法
// // let name = user.name
// // let age = user.age

// // let {name, age} = user
// let {name: uname, age: uage} = user
// console.log(uname, uage)

//字符串的解构赋值
// let str = 'imooc'

// // for(let i = 0; i < str.length; i++) {
// // console.log(str[i])
// // }

// let [a, b, c, d, e] = str
// console.log(a,b,c,d,e)

// let [a, b, c = 9] = [4, 5]
// console.log(a, b, c)

// let {name, age = 18} = {
// name: 'hekeying',
// // age: 28
// }

// console.log(name, age)

// function foo() {
// console.log(123)
// }

// let [a = foo()] = [] // 惰性 如果右边值为空 则左边值为foo里的值

// function foo([a, b, c]) {
// console.log(a, b, c)
// }

// let arr = [1, 2, 3]
// foo(arr)
// function foo({name, age, school = 'beijing'}) {
// console.log(name, age, school)
// }

// let obj = {
// name: 'hekeying',
// age: 28,
// school: 'qinghua'
// }
// foo(obj)

// function foo() {
// let obj = {
// name: 'hekeying',
// age: 28,
// school: 'qinghua'
// }
// return obj
// }


// let {name, age, school} = foo()
// console.log(name, age, school)

//提取JSON数据

// let json = '{"a": "hello", "b": "world"}'
// let {a, b} = JSON.parse(json)

// console.log(a, b)

推荐阅读