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 相关文章推荐
Jquery异步请求数据实例代码
Dec 28 Javascript
通过遮罩层实现浮层DIV登录的js代码
Feb 07 Javascript
javascript实现右侧弹出“分享到”窗口效果
Feb 01 Javascript
JavaScript面试题大全(推荐)
Sep 22 Javascript
html+javascript+bootstrap实现层级多选框全层全选和多选功能
Mar 09 Javascript
JavaScript选取(picking)和反选(rejecting)对象的属性方法
Aug 16 Javascript
微信小程序wx.request实现后台数据交互功能分析
Nov 25 Javascript
使用 vue 实现灭霸打响指英雄消失的效果附demo
May 06 Javascript
vue把输入框的内容添加到页面的实例讲解
Nov 11 Javascript
Vue 实现CLI 3.0 + momentjs + lodash打包时优化
Nov 13 Javascript
JavaScript正则表达式验证登录实例
Mar 18 Javascript
jquery插件实现代码雨特效
Apr 24 jQuery
基于原生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
php 随机记录mysql rand()造成CPU 100%的解决办法
2010/05/18 PHP
php下Memcached入门实例解析
2015/01/05 PHP
php+html5实现无刷新图片上传教程
2016/01/22 PHP
详解在PHP的Yii框架中使用行为Behaviors的方法
2016/03/18 PHP
thinkPHP商城公告功能开发问题分析
2016/12/01 PHP
laravel 数据验证规则详解
2019/10/23 PHP
javascript实现unicode和字符的互相转换
2007/07/18 Javascript
javascript 表单的友好用户体现
2009/01/07 Javascript
Mootools 1.2教程(3) 数组使用简介
2009/09/14 Javascript
jquery 的 $("#id").html() 无内容的解决方法
2010/06/07 Javascript
js正则表达exec与match的区别说明
2014/01/29 Javascript
Jquery Easyui表单组件Form使用详解(30)
2016/12/19 Javascript
jQuery开源组件BootstrapValidator使用详解
2017/06/29 jQuery
vue toggle做一个点击切换class(实例讲解)
2018/03/13 Javascript
vue axios基于常见业务场景的二次封装的实现
2018/09/21 Javascript
轻量级富文本编辑器wangEditor结合vue使用方法示例
2018/10/10 Javascript
[04:45]DOTA2-DPC中国联赛正赛 iG vs LBZS 赛后选手采访
2021/03/11 DOTA
python轻松查到删除自己的微信好友
2016/01/10 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
2017/01/28 Python
python使用Apriori算法进行关联性解析
2017/12/21 Python
python实现转圈打印矩阵
2019/03/02 Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
2019/12/26 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
2020/04/27 Python
Python sqlalchemy时间戳及密码管理实现代码详解
2020/08/01 Python
日本小田急百货官网:Odakyu
2018/07/19 全球购物
英国女性时尚品牌:Apricot
2018/12/04 全球购物
葡萄牙航空官方网站:TAP Air Portugal
2019/10/31 全球购物
公司营业员的工作总结自我评价
2013/10/05 职场文书
陈欧的广告词
2014/03/18 职场文书
党的群众路线调研报告
2014/11/03 职场文书
个人总结与自我评价
2015/02/14 职场文书
2015年百日安全活动总结
2015/03/26 职场文书
房产电话营销开场白
2015/05/29 职场文书
Python文件的操作示例的详细讲解
2021/04/08 Python
MySQL 数据类型详情
2021/11/11 MySQL
Python实现聚类K-means算法详解
2022/07/15 Python