js对象模式如何理解

928 js对象模式如何理解-大盘站插图

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

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

实例

test("valueobject",()=>{
letinput='{'
lety=match(input)
letv=y({)
letw=y({x:0)

expect(v).toEqual(true)
expect(w).toEqual(false)
)

test("objectELLIPSIS",()=>{
letinput='{...'
lety=match(input)
letv=y({)
letw=y({x:0)
letp=y([])

expect(v).toEqual(true)
expect(w).toEqual(true)
expect(p).toEqual(false)

)

test("objectproperties",()=>{
letinput='{x'
lety=match(input)
letv=y({x:0)
letw=y([null,1])

expect(v).toEqual(true)
expect(w).toEqual(false)
)

test("objectpropertiesELLIPSIS",()=>{
letinput='{x,...'
lety=match(input)
letv=y({x:0,y:1)
letw=y({)

expect(v).toEqual(true)
expect(w).toEqual(false)
)
test("propertiespropertiesprop",()=>{
letinput='{x,y'
lety=match(input)
letv=y({x:0,y:1)
letw=y({)

expect(v).toEqual(true)
expect(w).toEqual(false)
)

test("propkeyvalue",()=>{
letinput='{x:null'
lety=match(input)
letv=y({x:null)
letw=y([null,1])

expect(v).toEqual(true)
expect(w).toEqual(false)

)

test("keyQUOTE",()=>{
letinput='{"1":null'
lety=match(input)
letv=y({'1':null)
letw=y([null,1])

expect(v).toEqual(true)
expect(w).toEqual(false)
)