本篇内容主要讲解“怎么理解js对象模式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解js对象模式”吧!
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对象模式”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.py.cn/web/js/34570.html