JS Testing Properties 判断属性是否在对象里的方法
Testing Properties
To check whether an object has a property with a given name. You can do this with the in operator, with the hasOwnProperty() and propertyIsEnumerable() methods,
在JS中判断一个对象是否包含某个属性,可以使用 in,hasOwnProperty() and propertyIsEnumerable()
or simply by querying the property.
或者直接使用查询属性。
in--It returns true if the object has an own property or an inherited property
用In,当前对象存在或者有继承,就返回true。
hasOwnProperty() --To test whether that object has an own property with the given name. It returns false for inherited properties
用hasOwnProperty() ,只关心本对象,不关心继承来的属性。
propertyIsEnumerable()--The propertyIsEnumerable() refines the hasOwnProperty() test. It returns true only if the named property is an own property and its enumerable attribute is true.
用propertyIsEnumerable() ,和hasOwnProperty() 这个类似,只是要求 属性可枚举。
Instead of using the in operator it is often sufficient to simply query the property and use !== to make sure it is not undefined
o.x !== undefined; // true: o has a property x
替代In的最简单办法就是 query + !==Undefined
in can distinguish between properties that do not exist and properties that exist but have been set to undefined.
in 有个好处就是还能区分到底属性的值是undefined还是本身就不存在。
以上这篇JS Testing Properties 判断属性是否在对象里的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
- 韩国全球首测5G网络下自动驾驶 为汽车安全保驾护航的竟是路灯
- 通过与Quickbuild和Mist.io的持续集成实现云管理和使用监控
- .NET Core的文件系统[1]:读取并监控文件的变化
- ASP.NET MVC以ValueProvider为核心的值提供系统: ValueProviderFactory
- 云本机应用程序成熟度的模型
- 如何利用ETW(Event Tracing for Windows)记录日志
- 如何利用ETW(Event Tracing for Windows)记录日志
- 如何利用ETW(Event Tracing for Windows)记录日志
- ASP.NET MVC以ValueProvider为核心的值提供系统: DictionaryValueProvider
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
- .NET Core的文件系统[2]:FileProvider是个什么东西?
- Python多线程怎样优雅的响应中断异常
- .NET Core的文件系统[3]:由PhysicalFileProvider构建的物理文件系统
- .NET Core的文件系统[4]:由EmbeddedFileProvider构建的内嵌(资源)文件系统
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CVE-2020-14645 Weblogic远程代码执行漏洞分析
- PaGoDo:一款功能强大的被动式Google Dork
- 微软轻量级系统监控工具sysmon原理与实现完全分析
- 用Windows电脑训练深度学习模型?超详细配置教程来了
- rConfig中的远程代码执行漏洞分析
- CVE-2020-9964:iOS中的信息泄露漏洞分析
- ReconSpider:一款功能强大的高级OSINT框架
- Python 3.9来了!这十个新特性值得关注
- IRFuzz:一款基于YARA规则的文档文件扫描工具
- 内网渗透测试研究:从NTDS.dit获取域散列值
- 腾讯云大禹高防IP之客户端获取真实IP
- 终极解密输入网址按回车到底发生了什么
- Kafka核心原理的秘密,藏在这 17 张图中
- 国庆肝了8天整整2W字的数据库知识点
- MySQL事务与MVCC如何实现的隔离级别