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

边栏推荐
- Win32Exception (0x80004005): 组策略阻止了这个程序。要获取详细信息,请与系统管理员联系。
- September 29, 2020 non commodity templating code level rapidjson Library
- Picture verification code control
- std::make_ shared<T>/std::make_ Unique < T> and std:: shared_ ptr<T>/std::unique_ The difference and relation between PTR < t >
- 在VMware workstation中安装WMware ESXi 6.5.0并进行配置
- 云主机端口扫描
- 【高等数学】目录
- 2019.10.6 training summary
- std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
- 产品力不输比亚迪,吉利帝豪L雷神Hi·X首月交付1万台
猜你喜欢

《CLR via C#》读书笔记-单实例应用程序

Solve the problem that zxing's QR code contains Chinese garbled code

AGCTFb部分题解

Linux下Redis安装及集群搭建

Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX

Downloading and installing VMware (basic idea + detailed process)

BUUCTF--新年快乐

在VMware workstation中安装WMware ESXi 6.5.0并进行配置

WinForm uses zxing to generate QR code

Common usage of LINQ in C #
随机推荐
Text of the basic component of the shutter
Findbugs修改总结
Dynamic planning summary
View CSDN blog rankings
BUUCTF RE-easyre
工具箱之 IKVM.NET 项目新进展
October 17, 2020: question brushing 1
QGIS mapping
云主机端口扫描
BUUCTF--内涵的软件
PGP在加密技术中的应用
Reading notes of CLR via C -clr boarding and AppDomain
给定两个整形变量的值,将两个值的内容进行交换 (C语言)
打印100~200之间的素数(C语言)
解决zxing的QR码包含中文时乱码的问题
BUUCTF--新年快乐
IIS服务器相关错误
Call another interface button through win32API
2020-09-18 referer authentication URL escape
arcgis创建postgre企业级数据库