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 相关文章推荐
firefox中JS读取XML文件
Dec 21 Javascript
Track Image Loading效果代码分析
Aug 13 Javascript
jQuery阻止同类型事件小结
Apr 19 Javascript
js实现匹配时换色的输入提示特效代码
Aug 17 Javascript
JavaScript中Textarea滚动条不能拖动的解决方法
Dec 15 Javascript
浅析$(function) ready和onload 的区别
Sep 03 Javascript
Bootstrap源码解读导航(6)
Dec 23 Javascript
jQuery使用siblings获取某元素所有同辈(兄弟姐妹)元素用法示例
Jan 30 Javascript
vue中Axios的封装与API接口的管理详解
Aug 09 Javascript
微信小程序的tab选项卡的实现效果
May 15 Javascript
如何在JavaScript中谨慎使用代码注释
Jun 21 Javascript
微信小程序如何引用外部js,外部样式,公共页面模板
Jul 23 Javascript
基于原生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
ThinkPHP CURD方法之table方法详解
2014/06/18 PHP
jquery-easyui关闭tab自动切换到前一个tab
2010/07/29 Javascript
jQuery获取文本节点之 text()/val()/html() 方法区别
2011/03/01 Javascript
js 关键词高亮(根据ID/tag高亮关键字)案例介绍
2013/01/21 Javascript
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
js 对小数加法精度处理示例说明
2013/12/27 Javascript
Javascript变量的作用域和作用域链详解
2015/04/02 Javascript
JSON对象 详解及实例代码
2016/10/18 Javascript
jQuery利用sort对DOM元素进行排序操作
2016/11/07 Javascript
Angular ng-repeat指令实例以及扩展部分
2016/12/26 Javascript
微信小程序 扎金花简单实例
2017/02/21 Javascript
H5手机端多文件上传预览插件
2017/04/21 Javascript
nodejs的路径问题的解决
2018/06/30 NodeJs
原生js实现form表单序列化的方法
2018/08/02 Javascript
layui点击左侧导航栏,实现不刷新整个页面,只刷新局部的方法
2019/09/25 Javascript
layui的select联动实现代码
2019/09/28 Javascript
vue.js使用v-model实现父子组件间的双向通信示例
2020/02/05 Javascript
vue商城中商品“筛选器”功能的实现代码
2020/07/01 Javascript
python操作excel文件并输出txt文件的实例
2018/07/10 Python
python 获取键盘输入,同时有超时的功能示例
2018/11/13 Python
Python空间数据处理之GDAL读写遥感图像
2019/08/01 Python
python使用socket 先读取长度,在读取报文内容示例
2019/09/26 Python
Python 下载及安装详细步骤
2019/11/04 Python
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
wedgwood加拿大官网:1759年成立的英国国宝级陶瓷餐具品牌
2018/07/17 全球购物
莱德杯高尔夫欧洲官方商店:Ryder Cup Shop
2019/08/14 全球购物
俄罗斯GamePark游戏商店网站:购买游戏、游戏机和配件
2020/03/13 全球购物
某个公司的Java笔面试题
2016/03/11 面试题
《充气雨衣》教学反思
2014/04/07 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
青奥会口号
2014/06/12 职场文书
原料仓仓管员岗位职责
2014/07/08 职场文书
教师四风对照检查材料思想汇报
2014/09/17 职场文书
房屋分割离婚协议书范本
2014/12/01 职场文书
初中班级口号霸气押韵
2015/12/24 职场文书
计算机实训心得体会
2016/01/14 职场文书