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 相关文章推荐
拖动一个HTML元素
Dec 22 Javascript
Javascript 网页水印(非图片水印)实现代码
Mar 01 Javascript
在JS中解析HTML字符串示例代码
Apr 16 Javascript
JavaScript使用DeviceOne开发实战(三)仿微信应用
Dec 02 Javascript
javascript显示倒计时控制按钮的简单实现
Jun 07 Javascript
jquery 判断div show的状态实例
Dec 03 Javascript
开发Vue树形组件的示例代码
Dec 21 Javascript
小程序开发中如何使用async-await并封装公共异步请求的方法
Jan 20 Javascript
js如何实现元素曝光上报
Aug 07 Javascript
微信小程序获取用户信息及手机号(后端TP5.0)
Sep 12 Javascript
JS如何实现网站中PC端和手机端自动识别并跳转对应的代码
Jan 08 Javascript
vue-preview动态获取图片宽高并增加旋转功能的实现
Jul 29 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产生随机数的两种方法实例代码 输出随机IP
2011/04/08 PHP
ThinkPHP与PHPExcel冲突解决方法
2011/08/08 PHP
深入解析phpCB批量转换的代码示例
2013/06/27 PHP
Apache无法自动跳转却显示目录的解决方法
2020/11/30 PHP
浅谈PHP拦截器之__set()与__get()的理解与使用方法
2016/10/18 PHP
php 静态属性和静态方法区别详解
2017/04/09 PHP
详解no input file specified 三种解决方法
2019/11/29 PHP
Array.prototype.slice.apply的使用方法
2010/03/17 Javascript
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
JavaScript事件详细讲解
2016/06/27 Javascript
基于JavaScript实现移动端无限加载分页
2017/03/27 Javascript
详解Angular4 路由设置相关
2017/08/26 Javascript
判断js数据类型的函数实例详解
2019/05/23 Javascript
使用easyui从servlet传递json数据到前端页面的两种方法
2019/09/05 Javascript
Vue.js计算机属性computed和methods方法详解
2019/10/12 Javascript
使用p5.js实现动态GIF图片临摹重现
2019/10/23 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
wxPython中文教程入门实例
2014/06/09 Python
Python语言实现百度语音识别API的使用实例
2017/12/13 Python
Python爬取数据保存为Json格式的代码示例
2019/04/09 Python
pyinstaller参数介绍以及总结详解
2019/07/12 Python
使用python制作一个解压缩软件
2019/11/13 Python
关于Python3 lambda函数的深入浅出
2019/11/27 Python
python录音并调用百度语音识别接口的示例
2020/12/01 Python
pycharm中leetcode插件使用图文详解
2020/12/07 Python
海淘零差价,宝贝全球购: 宝贝格子
2016/08/24 全球购物
家用个人磨皮机:Trophy Skin
2017/03/30 全球购物
英国行业制服供应商:Alexandra
2019/09/14 全球购物
Talbots官网:美国成熟女装品牌
2019/11/15 全球购物
比较基础的php面试题及答案-编程题
2012/10/14 面试题
C#里面可以避免一个类被其他类继承么?如何?
2013/09/26 面试题
计算机多媒体专业自荐信
2014/07/04 职场文书
考试作弊被抓检讨书
2014/10/02 职场文书
2014年节能降耗工作总结
2014/12/11 职场文书
2016年党建工作简报
2015/11/26 职场文书
InterProcessMutex实现zookeeper分布式锁原理
2022/03/21 Java/Android