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动态设置div的值下例子
Oct 29 Javascript
js时钟翻牌效果实现代码分享
Jul 31 Javascript
javascript入门教程基础篇
Nov 16 Javascript
jQuery插件EasyUI校验规则 validatebox验证框
Nov 29 Javascript
node.js中 stream使用教程
Aug 28 Javascript
AngularJS+bootstrap实现动态选择商品功能示例
May 17 Javascript
js 原生判断内容区域是否滚动到底部的实例代码
Nov 15 Javascript
手写简单的jQuery雪花飘落效果实例
Apr 22 jQuery
深入理解vue-class-component源码阅读
Feb 18 Javascript
vue中axios实现数据交互与跨域问题
May 12 Javascript
element-ui table行点击获取行索引(index)并利用索引更换行顺序
Feb 27 Javascript
vue使用screenfull插件实现全屏功能
Sep 17 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
PHP中显示格式化的用户输入
2006/10/09 PHP
第一节--面向对象编程
2006/11/16 PHP
PHP中的替代语法简介
2014/08/22 PHP
php+ajax实现异步上传文件或图片功能
2017/07/18 PHP
Javascript的时间戳和php的时间戳转换注意事项
2013/04/12 Javascript
使用js解决由border属性引起的div宽度问题
2013/11/26 Javascript
Javascript中的包装类型介绍
2015/04/02 Javascript
郁闷!ionic中获取ng-model绑定的值为undefined如何解决
2016/08/27 Javascript
node.js实现登录注册页面
2017/04/08 Javascript
vuejs2.0子组件改变父组件的数据实例
2017/05/10 Javascript
vue源码解析之事件机制原理
2018/04/21 Javascript
微信小程序websocket实现即时聊天功能
2019/05/21 Javascript
ES6中字符串的使用方法扩展
2019/06/04 Javascript
JavaScript常用工具函数大全
2020/05/06 Javascript
python基础教程之自定义函数介绍
2014/08/29 Python
python中遍历文件的3个方法
2014/09/02 Python
django限制匿名用户访问及重定向的方法实例
2018/02/07 Python
Python中生成器和迭代器的区别详解
2018/02/10 Python
python中的数组赋值与拷贝的区别详解
2019/11/26 Python
tensorflow转换ckpt为savermodel模型的实现
2020/05/25 Python
python numpy实现rolling滚动案例
2020/06/08 Python
解决keras backend 越跑越慢问题
2020/06/18 Python
13个Pandas实用技巧,助你提高开发效率
2020/08/19 Python
python 将列表里的字典元素合并为一个字典实例
2020/09/01 Python
python自动化办公操作PPT的实现
2021/02/05 Python
安全生产目标责任书
2014/04/14 职场文书
教师廉洁自律承诺书
2014/05/26 职场文书
雷人标语集锦
2014/06/19 职场文书
博士生求职信
2014/07/06 职场文书
消防志愿者活动方案
2014/08/23 职场文书
民族学专业大学生职业规划范文:清晰未来的构想
2014/09/20 职场文书
2014矛盾纠纷排查调处工作总结
2014/12/09 职场文书
2014年英语工作总结
2014/12/20 职场文书
长城英文导游词
2015/01/30 职场文书
学校教学工作总结2015
2015/05/19 职场文书
Python制作表白爱心合集
2022/01/22 Python