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 相关文章推荐
Apply an AutoFormat to an Excel Spreadsheet
Jun 12 Javascript
屏蔽网页右键复制和ctrl+c复制的js代码
Jan 04 Javascript
为JS扩展Array.prototype.indexOf引发的问题探讨及解决
Apr 24 Javascript
jquery中animate的stop()方法作用实例分析
Jan 30 Javascript
简介JavaScript中Math.cos()余弦方法的使用
Jun 15 Javascript
angularjs 源码解析之injector
Aug 22 Javascript
Bootstrap如何激活导航状态
Mar 22 Javascript
微信小程序多列选择器range-key使用详解
Mar 30 Javascript
bootstrap table方法之expandRow-collapseRow展开或关闭当前行数据
Aug 09 Javascript
详解ES6中的代理模式——Proxy
Jan 08 Javascript
Intellij IDEA搭建vue-cli项目的方法步骤
Oct 20 Javascript
通过vue写一个瀑布流插件代码实例
Sep 07 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 连接Oracle数据库的详细教程[全]
2012/07/16 PHP
php实现倒计时效果
2015/12/19 PHP
windows7配置Nginx+php+mysql的详细教程
2016/09/04 PHP
比较简单实用的使用正则三种版本的js去空格处理方法
2007/11/18 Javascript
javascript高亮效果的二种实现方法
2008/09/14 Javascript
js动态生成指定行数的表格
2013/07/11 Javascript
jQuery中校验时间格式的正则表达式小结
2013/09/22 Javascript
将字符串中由空格隔开的每个单词首字母大写
2014/04/06 Javascript
jquery中的常用事件bind、hover、toggle等示例介绍
2014/07/21 Javascript
基于jQuery实现的向下滑动二级菜单效果代码
2015/08/31 Javascript
javascript中sort排序实例详解
2016/07/24 Javascript
AngularJs 动态加载模块和依赖
2016/09/15 Javascript
详解jQuery简单的表格应用
2016/12/16 Javascript
微信小程序 天气预报开发实例代码源码
2017/01/20 Javascript
js 递归和定时器的实例解析
2017/02/03 Javascript
JavaScript设计模式之观察者模式(发布订阅模式)原理与实现方法示例
2018/07/27 Javascript
微信小程序云开发修改云数据库中的数据方法
2019/05/18 Javascript
JavaScript事件冒泡机制原理实例解析
2020/01/14 Javascript
js表达式与运算符简单操作示例
2020/02/15 Javascript
详解elementUI中input框无法输入的问题
2020/04/27 Javascript
vue实现PC端分辨率适配操作
2020/08/03 Javascript
详解详解Python中writelines()方法的使用
2015/05/25 Python
django文档学习之applications使用详解
2018/01/29 Python
在python中bool函数的取值方法
2018/11/01 Python
python将字符串转变成dict格式的实现
2019/11/18 Python
python3中利用filter函数输出小于某个数的所有回文数实例
2019/11/24 Python
django xadmin中form_layout添加字段显示方式
2020/03/30 Python
Python3交互式shell ipython3安装及使用详解
2020/07/11 Python
几款好用的python工具库(小结)
2020/10/20 Python
python tkinter实现连连看游戏
2020/11/16 Python
HTML5新控件之日期和时间选择输入的实现代码
2018/09/13 HTML / CSS
HTML中使用SVG与SVG预定义形状元素介绍
2013/06/28 HTML / CSS
Servlet如何得到客户端机器的信息
2014/10/17 面试题
办护照工作证明范本
2014/01/14 职场文书
安全责任协议书
2014/04/21 职场文书
优秀教师自我评价范文
2014/09/27 职场文书