
js的各种数据类型判断(in、hasOwnProperty)
发布日期:2021-05-07 19:31:47
浏览次数:9
分类:技术文章
本文共 2774 字,大约阅读时间需要 9 分钟。
1.typeof
typeof 用来判断各种数据类型,有两种写法:typeof xxx , typeof(xxx)
例如:typeof 2 输出 number typeof NaN 输出 numbertypeof null 输出 object typeof {} 输出 object typeof [] 输出 object typeof (function(){}) 输出 function typeof undefined 输出 undefined typeof '222' 输出 string typeof true 输出 boolean
这里面包含了js里面的五种数据类型 number、string、boolean、 undefined、object 和函数类型 function
2. instanceof
判断已知对象类型的方法.instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; var f = function(){this.name="22";}; console.log(c instanceof Array) //true console.log(d instanceof Date) //true console.log(e instanceof Function) //true // console.log(f instanceof function ) //false
3.constructor
根据对象的constructor判断,返回对创建此对象的数组函数的引用。
var c= [1,2,3]; var d = new Date(); var e = function(){alert(111);}; alert(c.constructor === Array) ----------> true alert(d.constructor === Date) -----------> true alert(e.constructor === Function) -------> true //注意: constructor 在类继承时会出错
4.prototype
所有数据类型均可判断:Object.prototype.toString.call
这是对象的一个原生原型扩展函数,用来更精确的区分数据类型。var gettype=Object.prototype.toString gettype.call('aaaa') 输出 [object String] gettype.call(2222) 输出 [object Number] gettype.call(true) 输出 [object Boolean] gettype.call(undefined) 输出 [object Undefined] gettype.call(null) 输出 [object Null] gettype.call({}) 输出 [object Object] gettype.call([]) 输出 [object Array] gettype.call(function(){}) 输出 [object Function]
其实js 里面还有好多类型判断 [object HTMLDivElement] div 对象 , [object HTMLBodyElement] body 对象 ,[object Document](IE)或者 [object HTMLDocument](firefox,google) ……各种dom节点的判断,这些东西在我们写插件的时候都会用到。
可以封装的方法如下 :
var gettype=Object.prototype.toString var utility={ isObj:function(o){ return gettype.call(o)=="[object Object]"; }, isArray:function(o){ return gettype.call(o)=="[object Array]"; }, isNULL:function(o){ return gettype.call(o)=="[object Null]"; }, isDocument:function(){ return gettype.call(o)=="[object Document]"|| [object HTMLDocument]; } ........ }
ES6的 in(可以判断原型链上)
in是用来判断对象或者数组中是否存在某个值的。我们先来看一下用in如何判断对象里是否有某个值。
let obj={ a:'muzidigbig', b:'木子大大'}console.log('a' in obj); //true
数组判断
以前会使用length属性进行判断,为0表示没有数组元素。但是这并不准确,或者说真实开发中有弊端。
let arr=[,,,,,];console.log(0 in arr); //false let arr1=['muzidigbig','木子大大',,];console.log(2 in arr1); // falseconsole.log(1 in arr1); // true
hasOwnProperty
检查**对象自身**中是否含有该属性,如果有返回 true
function Monster(name, age, car) { this.name = name; this.age = age; this.car = car; } var niu = new Monster('niu', 29, 'biShui'); Monster.prototype.hobby = '吃肉'; // 考察 age console.log('hobby' in niu);//true console.log(niu.hasOwnProperty('hobby'));//false
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月18日 03时45分01秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
windows环境利用start命令实现微信多开
2019-03-04
「CF149D」括号涂色 区间DP好题
2019-03-04
树状数组 模板总结
2019-03-04
「NOI2015」程序自动分析 并查集题解
2019-03-04
[JSOI2008]Blue Mary的战役地图 Hash题解
2019-03-04
Ubuntu修改终端上显示的用户名和主机名(详细步骤)
2019-03-04
结构型设计在工作中的一些经验总结
2019-03-04
如何提升员工体验 助力企业业务增长?这个棘手的问题终于被解决了!
2019-03-04
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态
2019-03-04
“编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力!
2019-03-04
我是程序员,我用这种方式铭记历史
2019-03-04
CSDN湘苗培优|保持热情,告别平庸
2019-03-04
英特尔强势上新一大波数据产品,小伙伴们“奔走相告”…… | 极客头条
2019-03-04
微信小程序生命周期 / 页面的生命周期 / 页面的用户行为
2019-03-04
YbtOJ 递推算法课堂过关 例5 平铺方案【递推(简单DP)】
2019-03-04
YbtOJ hash和hash表课堂过关 例1 字符串哈希【hash】
2019-03-04
CSUST 2021 周赛 2 题解
2019-03-04
前后端数据交互之表单
2019-03-04
剑指offer JZ21 栈的压入弹出序列
2019-03-04
实现基于scrapy框架的天气预报爬虫hengYangSpaider @572311文
2019-03-04