品易云推流 关闭
文章详情页
文章 > JavaScript > js对象模式如何理解

js对象模式如何理解

js 对象模式

头像

小妮浅浅

2021-10-26 10:30:036112浏览 · 0收藏 · 0评论

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

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

实例

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教程

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图