JS Testing Properties 判断属性是否在对象里的方法


Posted in Javascript onOctober 01, 2017

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 判断属性是否在对象里的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js中的escape及unescape函数的php实现代码
Sep 04 Javascript
基于jquery自定义图片热区效果
Jul 21 Javascript
js模拟点击事件实现代码
Nov 06 Javascript
jQuery中的ajax async同步和异步详解
Sep 29 Javascript
深入理解jQuery之事件移除
Jun 02 Javascript
jquery自动补齐功能插件flexselect用法示例
Aug 06 Javascript
js改变透明度实现轮播图的算法
Aug 24 Javascript
BootStrap 可编辑表Table格
Nov 24 Javascript
create-react-app使用antd按需加载的样式无效问题的解决
Feb 26 Javascript
微信小程序防止多次点击跳转(函数节流)
Sep 19 Javascript
vue改变循环遍历后的数据实例
Nov 07 Javascript
在vue项目中封装echarts的步骤
Dec 25 Vue.js
基于原生js运动方式关键点的总结(推荐)
Oct 01 #Javascript
vuejs使用递归组件实现树形目录的方法
Sep 30 #Javascript
Easy UI动态树点击文字实现展开关闭功能
Sep 30 #Javascript
js实现轮播图的两种方式(构造函数、面向对象)
Sep 30 #Javascript
React实践之Tree组件的使用方法
Sep 30 #Javascript
JS动态添加的div点击跳转到另一页面实现代码
Sep 30 #Javascript
Node.js微信 access_token ( jsapi_ticket ) 存取与刷新的示例
Sep 30 #Javascript
You might like
PHP4与PHP3中一个不兼容问题的解决方法
2006/10/09 PHP
PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法
2011/08/22 PHP
mysql desc(DESCRIBE)命令实例讲解
2016/09/24 PHP
PHP数组中头部和尾部添加元素的方法(array_unshift,array_push)
2017/04/10 PHP
PHP7变量处理机制修改
2021/03/09 PHP
做网页的一些技巧
2007/02/01 Javascript
jquery利用event.which方法获取键盘输入值的代码
2011/10/09 Javascript
JS字符串拼接在ie中都报错的解决方法
2014/03/27 Javascript
js简单实现交换Li的值
2014/05/22 Javascript
在JS数组特定索引处指定位置插入元素
2014/07/27 Javascript
ubuntu下安装nodejs以及升级的办法
2015/05/08 NodeJs
JS简单实现多级Select联动菜单效果代码
2015/09/06 Javascript
只要1K 纯JS脚本送你一朵3D红色玫瑰
2016/08/09 Javascript
Vue实现点击时间获取时间段查询功能
2020/08/21 Javascript
react实现换肤功能的示例代码
2018/08/14 Javascript
详解Axios统一错误处理与后置
2018/09/26 Javascript
详解基于Vue,Nginx的前后端不分离部署教程
2018/12/04 Javascript
js实现从右往左匀速显示图片(无缝轮播)
2020/06/29 Javascript
JavaScript常用进制转换及位运算实例解析
2020/10/14 Javascript
Python制作爬虫采集小说
2015/10/25 Python
Django 路由控制的实现代码
2018/11/08 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
python3发送邮件需要经过代理服务器的示例代码
2019/07/25 Python
详解python中的模块及包导入
2019/08/30 Python
使用Python打造一款间谍程序的流程分析
2020/02/21 Python
Python ellipsis 的用法详解
2020/11/20 Python
python中的unittest框架实例详解
2021/02/05 Python
神经网络训练采用gpu设置的方式
2021/03/03 Python
英国Zoro工具:手动工具,电动工具和个人防护用品
2016/11/02 全球购物
Ruby如何创建一个线程
2013/03/10 面试题
普通党员个人对照检查材料
2014/09/18 职场文书
大学生党员自我评价
2015/03/04 职场文书
五一晚会主持词
2015/07/01 职场文书
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
CPU不支持Windows11系统怎么办
2021/11/21 数码科技
Redis基本数据类型List常用操作命令
2022/06/01 Redis