10、ES6 字符串的解构赋值

来源:互联网 发布:淘宝上色盲眼镜有用吗 编辑:程序博客网 时间:2024/06/06 16:55

字符串的解构赋值

       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>






原创粉丝点击