JavaScript笔记-解构赋值
https://www.liaoxuefeng.com/wiki/1022910821149312基于廖雪峰老师的教程所做的笔记。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>解构赋值操作展示</title> </head> <body>
<script>
console.log("解构数组:"); var [a,b,c]=["我是a","我是b","我是c"]; console.log(a); console.log(b); console.log(c);
var [d,e,f]=[,,"我是f"]; console.log(d); console.log(e); console.log(f);
console.log("解构一般对象:"); var obj={ name:"Sang", age:20, city:"Guoyang" };
var {name,age,city}=obj; console.log(name); console.log(age); console.log(city);
console.log("解构含有对象的对象:") var person={ lastName:"Sang", address:{ city:"Guoyang", town:"Chengxi" } }
var {lastName,address:{city,town}}=person; console.log(lastName); console.log(city); console.log(town);
console.log("变量交换:") var x=1,y=2; [x,y]=[y,x]; console.log(x); console.log(y);
console.log("获取当前页面域名、路径:") var{hostname:domain,pathname:path}=location; console.log(domain); console.log(path);
function display({lastname,firstname="Guorui"}){ console.log(lastname+firstname); }
display({lastname:"Sang"}); display({lastname:"Sang",firstname:"haha"});
</script> </body> </html>
|