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中的preventDefault与stopPropagation详解
Jan 29 Javascript
加随机数引入脚本不让浏览器读取缓存
Sep 04 Javascript
jQuery选择器用法实例详解
Dec 17 Javascript
JavaScript引用类型和基本类型详解
Jan 06 Javascript
浅谈移动端之js touch事件 手势滑动事件
Nov 07 Javascript
微信小程序 弹幕功能简单实例
Feb 14 Javascript
原生JavaScrpit中异步请求Ajax实现方法
Nov 03 Javascript
Vue按需加载的具体实现
Dec 02 Javascript
vue 将页面公用的头部组件化的方法
Dec 18 Javascript
在layui下对元素进行事件绑定的实例
Sep 06 Javascript
jQuery 动态粒子效果示例代码
Jul 07 jQuery
JS实现京东商品分类侧边栏
Dec 11 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
简单易用的计数器(数据库)
2006/10/09 PHP
采集邮箱的php代码(抓取网页中的邮箱地址)
2012/07/17 PHP
php动态实现表格跨行跨列实现代码
2012/11/06 PHP
php制作中间带自己定义图片二维码的方法
2014/01/27 PHP
ThinkPHP Mobile使用方法简明教程
2014/06/18 PHP
关于URL最大长度限制的相关资料查证
2014/12/23 PHP
详解PHP的Yii框架的运行机制及其路由功能
2016/03/17 PHP
PHP使用redis消息队列发布微博的方法示例
2017/06/22 PHP
用jQuery技术实现Tab页界面之二
2009/09/21 Javascript
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
2011/09/21 Javascript
Javascript图像处理—平滑处理实现原理
2012/12/28 Javascript
JavaScript 函数replace深入了解
2013/03/14 Javascript
关于jQuery参考实例 1.0 jQuery的哲学
2013/04/07 Javascript
没有document.getElementByName方法
2013/08/19 Javascript
Jquery遍历节点的方法小集
2014/01/22 Javascript
Bootstrap布局组件教程之Bootstrap下拉菜单
2016/06/12 Javascript
AngularJS实现数据列表的增加、删除和上移下移等功能实例
2016/09/05 Javascript
微信小程序入门教程
2016/11/18 Javascript
JavaScript 函数节流详解及方法总结
2017/02/09 Javascript
js中的this的指向问题详解
2019/08/29 Javascript
vue+Element中table表格实现可编辑(select下拉框)
2020/05/21 Javascript
Python检测字符串中是否包含某字符集合中的字符
2015/05/21 Python
Python实现计算最小编辑距离
2016/03/17 Python
python 实时遍历日志文件
2016/04/12 Python
深入探究Django中的Session与Cookie
2017/07/30 Python
Python分析学校四六级过关情况
2017/11/22 Python
python爬取网页转换为PDF文件
2018/06/07 Python
浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
2020/01/20 Python
N:Philanthropy官网:美国洛杉矶基础款服装
2020/06/09 全球购物
工程业务员工作职责
2013/12/07 职场文书
大学生简短的自我评价分享
2014/02/20 职场文书
卫校毕业生个人自我鉴定
2014/04/28 职场文书
贫困证明怎么写
2015/06/16 职场文书
生日赠语
2015/06/23 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
输入框跟随文字内容适配宽实现示例
2022/08/14 Javascript