10、ES6 字串的解構賦值
字串的解構賦值
1、字串也可以解構賦值:字串被轉換成了一個類似陣列的物件。
2、屬性解構賦值:類似陣列的物件都有一個length屬性,因此還可以對這個屬性解構賦值。
----------------------------------------------------------------字串的解構賦值----------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>字串的解構賦值</title>
<script src="js/traceur.js"></script>
<script src="js/bootstrap.js"></script>
<script type="text/traceur">
const [a,b,c,d,e]="Hello";
console.log(a); //H
console.log(b); //e
console.log(c); //l
console.log(d); //l
console.log(e); //o
//將字串轉換成了類似陣列
</script>
</head>
<body>
</body>
</html>
-----------------------------------字串的屬性解構賦值---------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>字串的屬性解構賦值</title>
<script src="js/traceur.js"></script>
<script src="js/bootstrap.js"></script>
<script type="text/traceur">
const {length:len}="Hello"; //將length給len或者說重新命名為len
console.log(len); //5
const {length}="hello world";
console.log(length); //11
//即可把字串看成是陣列按索引解構,也可看成是類似陣列物件的length屬性,取出字串的長度
</script>
</head>
<body>
</body>
</html>