当前位置:网站首页>Reprint: five methods to determine whether an object has attributes
Reprint: five methods to determine whether an object has attributes
2022-06-29 10:39:00 【Upwind elegance】
During programming development , We often need to determine whether an object has certain properties . ad locum , We list 了 5 A way to judge the properties of an object .
1、in Operator
If the specified attribute is in the specified object or its prototype chain , be in Operator return true.
usage :

however in Operator There is a drawback , That's it : If the attribute comes from the prototype of the object , It will still return true.

2、Reflect.has() Method
Reflect.has Method allows us to check whether the attribute is in the object . It looks like in Operator Work as a function .
usage :

3、 hasOwnProperty() Method
hasOwnProperty() Method returns a Boolean value , Indicates whether the object has the specified property as its own property ( Instead of inheriting it ).
It can correctly distinguish the attributes of the object itself from those of its prototype .
usage :

But this way of writing has a drawback , That is, if the object uses Object.create(null) Create words , Then you can't use this method .

4、Object.prototype.hasOwnProperty() Method
The solution to the previous problem is simple , We just need to use Object.prototype.hasOwnProperty. This method calls the built-in effective function directly , Skip prototype chain .

5、Object.hasOwn() Method
Because the previous methods are not elegant ,ES2022 There is a new proposal :Object.hasOwn
If the specified object has the specified attribute as its own attribute , be Object.hasOwn() Static method returns true. If the attribute is inherited or does not exist , Then the method returns false.
usage :

边栏推荐
- Analysis on the specific execution process of an insert statement in MySQL 8.0 (3)
- IO流总结
- 这个开源项目超哇塞,手写照片在线生成
- std::unique_ PTR < T> and boost:: scoped_ Particularity of PTR < t >
- Dynamic planning summary
- 在VMware workstation中安装WMware ESXi 6.5.0并进行配置
- 在实践中学习Spark计算框架(01)
- SQL Server 数据库的几种简单查询
- 2020-10-17:刷题1
- MySQL innodb每行数据长度的限制
猜你喜欢

Seaweedfs security configuration

IIS服务器相关错误

Linux下Redis安装及集群搭建

Basic operations during dev use

C#中Attribute(特性)

Rikka with cake (segment tree + segment tree)

October 17, 2020: question brushing 1

Analysis of BlockingQueue source code of AQS

打印100~200之间的素数(C语言)

Redis installation and cluster setup under Linux
随机推荐
攻防世界-Re-insfsay
September 21, 2020 referer string segmentation boost gateway code organization level
Hystrix fuse: Service fusing and service degradation
Design of intelligent test paper generation system
《CLR via C#》读书笔记-加载与AppDomain
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
软件测试模型(V模型和W模型)
UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记
在VMware workstation中安装WMware ESXi 6.5.0并进行配置
BUUCTF--reverse2
C#窗体向另一个窗体实时传值
stream流(Collectors)用法
September 25, 2020 noncopyable of boost library for singleton mode
F5 big IP Icontrol rest command execution (cve-2022-1388)
AQS之ReentrantLock源码解析
Fully understand the MESI cache consistency protocol
IO流总结
Win32exception (0x80004005): This program is blocked by group policy. For more information, contact your system administrator.
Oracle重置序列发生器(非重建)
arcgis创建postgre企业级数据库