• JS 解构赋值


    感谢原文作者:小火柴的蓝色理想
    原文链接:https://www.cnblogs.com/xiaohuochai/p/7243166.html

    介绍

    解构赋值语法是一种 Javascript ES6引入的表达式。通过解构赋值, 可以将属性/对象/数组中取出,赋值给其他变量。解构。解构是一种打破数据结构,将其拆分为更小部分的过程。

    对象解构

    基础语法
    let node = {
        type: "Identifier",
        name: "foo"
    };
    let { type, name } = node;
    console.log(type); // "Identifier"
    console.log(name); // "foo"
    
    向已声明的变量赋值
    let node = {
        type: "Identifier",
        name: "foo"
    },
    type = "Literal",
    name = 5;
    // 使用解构来分配不同的值
    ({ type, name } = node);
    console.log(type); // "Identifier"
    console.log(name); // "foo"
    

    注意:一定要用一对小括号包裹解构赋值语句,JS引擎将一对开放的花括号视为一个代码块。语法规定,代码块语句不允许出现在赋值语句左侧,添加小括号后可以将块语句转化为一个表达式,从而实现整个解构赋值过程

    任何可以使用值的地方都可以使用解构赋值表达式

    let node = {
        type: "Identifier",
        name: "foo"
    },
    type = "Literal",
    name = 5;
    function outputInfo(value) {
        console.log(value === node); // true
    }
    outputInfo({ type, name } = node);
    console.log(type); // "Identifier"
    console.log(name); // "foo"
    
    设置默认值

    注意:解构赋值表达式(也就是=右侧的表达式)如果为null或undefined会导致程序抛出错误。也就是说,任何尝试读取null或undefined的属性的行为都会触发运行时错误

    let node = {
        type: "Identifier",
        name: "foo"
    };
    let { type, name, value = true } = node;
    console.log(type); // "Identifier"
    console.log(name); // "foo"
    console.log(value); // true
    

    为变量value设置了默认值true,只有当node上没有该属性或者该属性值为undefined时该值才生效。此处没有node.value属性,因为value使用了预设的默认值

    为非同名局部变量赋值
    let node = {
        type: "Identifier",
        name: "foo"
    };
    let { type: localType, name: localName } = node;
    console.log(localType); // "Identifier"
    console.log(localName); // "foo"
    
    为非同名局部变量赋值并设置默认值

    当使用其他变量名进行赋值时也可以添加默认值,只需在变量名后添加等号和默认值即可

    let node = {
        type: "Identifier"
    };
    let { type: localType, name: localName = "bar" } = node;
    console.log(localType); // "Identifier"
    console.log(localName); // "bar"
    
    嵌套对象解构
    let node = {
        type: "Identifier",
        name: "foo",
        loc: {
            start: {
                line: 1,
                column: 1
            },
        end: {
            line: 1,
            column: 4
        }
    }
    };
    let { loc: { start }} = node;
    console.log(start.line); // 1
    console.log(start.column); // 1
    

    在上面的解构示例中,所有冒号前的标识符都代表在对象中的检索位置,其右侧为被赋值的变量名如果冒号后是花括号,则意味着要赋予的最终值嵌套在对象内部更深的层级中
    也可以使用一个与对象属性名不同的局部变量名

    let node = {
        type: "Identifier",
        name: "foo",
        loc: {
            start: {
                line: 1,
                column: 1
            },
            end: {
                line: 1,
                column: 4
            }
        }
    };
    // 提取 node.loc.start
    let { loc: { start: localStart }} = node;
    console.log(localStart.line); // 1
    console.log(localStart.column); // 1
    

    数组结构

    与对象解构的语法相比,数组解构就简单多了,它使用的是数组字面量,且解构操作全部在数组内完成,而不是像对象字面量语法一样使用对象的命名属性

    let colors = [ "red", "green", "blue" ];
    let [ firstColor, secondColor ] = colors;
    console.log(firstColor); // "red"
    console.log(secondColor); // "green"
    
    解构特定值
    let colors = [ "red", "green", "blue" ];
    let [ , , thirdColor ] = colors;
    console.log(thirdColor); // "blue"
    
    向已声明的变量赋值(设置默认值与对象解构一样)

    数组解构也可用于赋值上下文,但不需要用小括号包裹表达式,这一点与对象解构不同

    let colors = [ "red", "green", "blue" ],
    firstColor = "black",
    secondColor = "purple";
    [ firstColor, secondColor ] = colors;
    console.log(firstColor); // "red"
    console.log(secondColor); // "green"
    
    变量交换
    // 在 ES6 中互换值
    let a = 1,
        b = 2;
    [ a, b ] = [ b, a ];
    console.log(a); // 2
    console.log(b); // 1
    
    嵌套数组解构

    嵌套数组解构与嵌套对象解构的语法类似,在原有的数组模式中插入另一个数组模式,即可将解构过程深入到下一个层级

    let colors = [ "red", [ "green", "lightgreen" ], "blue" ];
    // 随后
    let [ firstColor, [ secondColor ] ] = colors;
    console.log(firstColor); // "red"
    console.log(secondColor); // "green"
    
    不定元素

    函数具有不定参数,而在数组解构语法中有一个相似的概念——不定元素。在数组中,可以通过...语法将数组中的其余元素赋值给一个特定的变量

    let colors = [ "red", "green", "blue" ];
    let [ firstColor, ...restColors ] = colors;
    console.log(firstColor); // "red"
    console.log(restColors.length); // 2
    console.log(restColors[0]); // "green"
    console.log(restColors[1]); // "blue"
    
    数组复制

    在ES5中,开发者们经常使用concat()方法来克隆数组

    // 在 ES5 中克隆数组
    var colors = [ "red", "green", "blue" ];
    var clonedColors = colors.concat();
    console.log(clonedColors); //"[red,green,blue]"
    

    在ES6中,可以通过不定元素的语法来实现相同的目标

    // 在 ES6 中克隆数组
    let colors = [ "red", "green", "blue" ];
    let [ ...clonedColors ] = colors;
    console.log(clonedColors); //"[red,green,blue]"
    

    混合解构

    具体还请查阅原文。

    其他解构

    字符串解构

    字符串也可以解构赋值。这是因为,字符串被转换成了一个类似数组的对象

    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"
    

    类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值

    const {length} = 'hello';
    console.log(length);//5
    
    数值和布尔值解构

    解构赋值时,如果等号右边是数值和布尔值,则会先转为对象

    let {toString:s1} = 123;
    console.log(s1 === Number.prototype.toString);//true
    let {toString:s2} = true;
    console.log(s2 === Boolean.prototype.toString);//true
    

    解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象。由于undefinednull无法转为对象,所以对它们进行解构赋值,都会报错

    let { prop: x } = undefined; // TypeError
    let { prop: y } = null; // TypeError
    
  • 相关阅读:
    Ubuntu8.04代码已冻结发布在即
    Ruby on rails开发从头来(五十) ActiveRecord基础(更新记录)
    Ruby on rails开发从头来(四十四) ActiveRecord基础(创建记录)
    Ruby on rails开发从头来(四十五) ActiveRecord基础(读取记录)
    网络恶搞“白送家产”:美国男子回家发现财产被搬空
    Ruby on rails开发从头来(四十三) ActiveRecord基础(连接数据库)
    Ruby on rails开发从头来(四十九) ActiveRecord基础(行数和再加载数据)
    Ruby on rails开发从头来(四十七) ActiveRecord基础(强大的find方法)
    Ruby on rails开发从头来(四十六) ActiveRecord基础(SQL和Active Record)
    简单三步堵死SQL Server注入漏洞
  • 原文地址:https://www.cnblogs.com/tfxz/p/12838739.html
Copyright © 2020-2023  润新知