《ES6标准入门(第3版)》学习笔记7:chapter_3-3 字符串的解构赋值 and 3_4 数值和布尔值的解构赋值

让学习“上瘾”,成为更好的自己!!!
一,字符串的解构赋值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>字符串的解构赋值</title>
</head>
<body>
    
    <script>
    // 字符串也可以解构赋值 --> 此时的字符串被转换成一个类似数组的对象

    const [a, b, c, d] = 'hello';
    console.log(a);
    console.log(b);
    console.log(c);

    // 类似于数组的对象都有一个length属性
    let {length: len} = 'hello';
    console.log(len);   // 5
    

    
    </script>



</body>
</html>

二,数值和布尔值的解构赋值

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数值和布尔值的解构赋值</title>
</head>

<body>
    <script>
        // 结构赋值时,如果等号右边是“数值”和“布尔值”,则会先装换为对象
        // let {toString: s} = 1231;
        // console.log(s === Number.prototype.toString); // true

        let {toString: s} = true;
        console.log(s === Boolean.prototype.toString);   // true
        

        var aa = new Number(33);
        console.dir(aa);

        // 上面的代码中,数值和布尔值的包装对象都有toString属性,因此变量s可以取到值

        // 【解构赋值的规则】
        //  只要等号右边的值不是对象或者数组,就先将其转换为对象,undefined and null无法转为对象,对他们进行解构赋值都会报错
        let {prop1: x} = undefined;
        let {prop2: y} = null;
        //  Cannot destructure property `prop1` of 'undefined' or 'null'.




    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值