当前位置:网站首页>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 :

边栏推荐
- I would like to know how to open an account for free online stock registration? In addition, is it safe to open a mobile account?
- 2019.10.6 training summary
- InnoDB in MySQL_ page_ Cleaners details
- Virtual machine port scanning
- Web漏洞手动检测分析
- BUUCTF--新年快乐
- 产品力不输比亚迪,吉利帝豪L雷神Hi·X首月交付1万台
- 打印100~200之间的素数(C语言)
- Software test model (V model and W model)
- Attribute in C
猜你喜欢

全面理解MESI缓存一致性协议

Voir le classement des blogs pour csdn

Call another interface button through win32API

Recyclerview sticky (suspended) head

由ASP.NET Core根据路径下载文件异常引发的探究

Seaweedfs security configuration

October 17, 2020: question brushing 1

Analysis of reentrantlock source code of AQS

September 29, 2020 non commodity templating code level rapidjson Library

Solve the problem that zxing's QR code contains Chinese garbled code
随机推荐
2019.10.30 learning summary
std::unique_ PTR < T> and boost:: scoped_ Particularity of PTR < t >
IIS server related error
這個開源項目超哇塞,手寫照片在線生成
Ce projet Open source est super wow, des photos manuscrites sont générées en ligne
攻防世界-Re-insfsay
The process of updating a record in MySQL
AQS之BlockingQueue源码解析
Analysis on the specific execution process of an insert statement in MySQL 8.0 (2)
IO流总结
F5 big IP Icontrol rest command execution (cve-2022-1388)
This open source project is super wow, and handwritten photos are generated Online
Voir le classement des blogs pour csdn
《CLR via C#》读书笔记-加载与AppDomain
LVGL库入门教程 - 动画
《CLR via C#》读书笔记-单实例应用程序
MySQL innodb每行数据长度的限制
C#窗体向另一个窗体实时传值
C#中IEqualityComparer接口的实现
《CLR via C#》读书笔记-CLR寄宿与AppDomain