• 技术文章 >Web开发 >JavaScript

    js对象模式如何理解

    小妮浅浅小妮浅浅2021-10-26 10:30:03原创11816

    1、匹配对象。如果有省略号,对象可以有更多的属性。

    2、只检测自己的属性(Object.keys),忽略原型中的属性。对象语法支持特殊识别属性,快速属性,属性不支持尾逗号。

    实例

    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

    test("value object", () => {

        let input = '{}'

        let y = match(input)

        let v = y({})

        let w = y({ x: 0 })

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

    })

      

    test("object ELLIPSIS", () => {

        let input = '{...}'

        let y = match(input)

        let v = y({})

        let w = y({ x: 0 })

        let p = y([])

      

        expect(v).toEqual(true)

        expect(w).toEqual(true)

        expect(p).toEqual(false)

      

    })

      

    test("object properties", () => {

        let input = '{x}'

        let y = match(input)

        let v = y({ x: 0 })

        let w = y([null, 1])

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

    })

      

    test("object properties ELLIPSIS", () => {

        let input = '{x,...}'

        let y = match(input)

        let v = y({ x: 0, y: 1 })

        let w = y({})

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

    })

    test("properties properties prop", () => {

        let input = '{x,y}'

        let y = match(input)

        let v = y({ x: 0, y: 1 })

        let w = y({})

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

    })

      

    test("prop key value", () => {

        let input = '{x:null}'

        let y = match(input)

        let v = y({ x: null })

        let w = y([null, 1])

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

      

    })

      

    test("key QUOTE", () => {

        let input = '{"1":null}'

        let y = match(input)

        let v = y({ '1': null })

        let w = y([null, 1])

         

        expect(v).toEqual(true)

        expect(w).toEqual(false)

    })

    以上就是js对象模式的理解,希望对大家有所帮助。更多js学习指路:js教程

    专题推荐:js 对象模式
    上一篇:js数组模式是什么 下一篇:js逻辑操作符的介绍

    相关文章推荐

    • js中var如何声明作用域• js中let和var的区别• js中对象的两种属性• js策略模式是什么• js中typeof操作符是什么• js数组中filter方法的使用• js数组中find方法的介绍• js数组中reduce的用法• js函数声明的提升顺序• js如何自定义构造函数创建对象• js标识符模式的介绍• js数组模式是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网