解构赋值:
注意:
1、左右两边结构必须一样 练习1,2,3
2、右边必须是个东西(有值)练习4
3、声明和赋值不能分开(必须在一句话里完成)练习5
/* 练习1: // let arr = [1,2,3]; // let a = arr[0]; // let b = arr[1]; // let c = arr[2]; // console.log(a,b,c); // 输出:1 2 3 //解构赋值 let [a,b,c] = [1,2,3]; console.log(a,b,c); // 输出:1 2 3 */ /*练习2: let {a,c,d} = {a:12, c:2, d:4}; console.log(a,c,d); // 输出:12 2 4 */ /*练习3: 粒度 json arr let [{a,b},[c,d,e],num,str] =[{a:12, b:33},[12,4,3],8,‘saf‘]; console.log(a,b,c,d,e,num,str); //输出:12 33 12 4 3 8 "saf" let [json,arr,num,str] =[{a:12, b:33},[12,4,3],8,‘saf‘]; console.log(json,arr,num,str); // 输出 {a: 12, b: 33} (3) [12, 4, 3] 8 "saf" */ /*练习4: let {a,b} = {12, 5}; // 对象 = {属性:值} console.log(a,b); // 输出:Uncaught SyntaxError: Unexpected token , */ /*练习5: let [a,b]; [a,b] = [12,5]; console.log(a,b); // 输出:Uncaught SyntaxError: Missing initializer(初始值) in destructuring declaration */
原文地址:https://www.cnblogs.com/sylys/p/11635745.html
时间: 2024-10-08 22:09:35