一、先介绍下5种原始类型
JavaScript中5种原始类型为string,number,boolean,undefined,null
var name = "Jack"; var age = 32; var single = false; var app; //undefined console.log(typeof name); //string console.log(typeof age); //number console.log(typeof single); //boolean console.log(typeof app); //undefined console.log(typeof null); //object
发现除null外的其他4种基本类型均可以用typeof来识别:
if(typeof name === "string") { name += "Zhang"; } if(typeof age === "number") { age++; } if(typeof single === "boolean" && single) { … } if(typeof app === "undefined") { app = {}; }
因为typeof null会得到object,所以直接用===来检测null:
if(el === null) { … }
二、对象
JavaScript的对象包括内置对象(Date,RegExp ,Error等)和自定义对象。
(注意,Function和Array虽然也都是内置对象,但下一节单独讲)
对象不能像基本类型那样用typeof来检测了,因为检测出来的结果都是object:
console.log(typeof new Date()); //object console.log(typeof new RegExp()); //object console.log(typeof new Error()); //object console.log(typeof new Person()); //用typeof检测出自定义对象也是object
要改用instanceof来检测:
var date = new Date(); var reg = new RegExp(); var err = new Error(); var me = new Person(); if(date instanceof Date) { //检测日期 year = date.getFullYear(); } if(reg instanceof RegExp) { //检测正则表达式 reg.test(...); } if(err instanceof Error) { //检测异常 throw err; } if(me instanceof Person) { //检测自定义对象 ... }
但自定义对象有个问题,假设浏览器frameA里和frameB里都定义了Person。 frameA里定义了me对象,用me instanceof Person
检测出来为true。但当自定义对象me传给frameB后,在frameB里instanceof会是false。
本节一开头就说了,Function和Array虽然也都是内置对象,但留到下一节讲。原因就是Function和Array也有和自定义对象相同的上述问题。因此Function和Array一般不用instanceof
三、Function
上面说了用instanceof检测Function不能跨frame。因此用typeof来检测,它可跨frame:
var func = function(){}; if(typeof func === 'function') { … }
但IE8以前用typeof来检测DOM系函数会得到object,因此IE8以前改用in:
console.log(typeof document.getElementById); //object,不是function console.log(typeof document.getElementsByTagName); //object,不是function console.log(typeof document.createElement); //object,不是function //IE8以前的IE浏览器,要改用in来检测是否支持DOM函数 if("getElementById" in document) { … } if("getElementsByTagName" in document) { … } if("createElement" in document) { … }
四、Array
上面说了用instanceof检测Array不能跨frame。ES5之前都自定义检测方法。其中最精确的方法:依赖Array的toString会返回固定字符串”[Object Array]”的事实来检测:
function isArray(arr) { return Object.prototype.toString.call(arr) === "[Object Array]"; }
该方法精确且优雅,因此被很多库所采纳,最终在ES5被作为isArray方法引入了Array,参照MDN。现在你不需要自定义检测方法了,直接用isArray()即可。
其他检测方法,都各有缺陷,不能100%精确。但作为一种思路是可以借鉴的。例如依赖Array是唯一包含sort方法的对象的事实来检测:
function isArray(arr) { return typeof arr.sort === "function"; }
如果是自定义对象也定义了sort方法,该方法就失效了。
五、属性
检测属性是否在实例对象中应该用hasOwnProperty。如果你不关心属性是在实例对象中还是在原型对象中,可以简单点用in
例如检测字面量对象属性:
var Person = { name: "Jack", age: 33 }; if("name" in Person) { … } //true if(Person.hasOwnProperty("name")) { … } //true
例如实例对象属性:
var Person = function (name, age) { this.name = name; this.age = age; }; Person.prototype.location = "Shanghai"; var me = new Person("Jack", 33) if("name" in me) { … } //true if(me.hasOwnProperty("name")) { … } //true if("location" in me) { … } //true if(me.hasOwnProperty("location")) { … }//false
除此之外其他方法都不好:
if (object[propName]) //Not Good,你怎么知道属性值不是0或1? if (object[propName] === null) //Not Good,你怎么知道属性值不是null? if (object[propName] === undefined) //Not Good,你怎么知道属性值不是undefined?
总结
用typeof检测string,number,boolean,undefined,Function
用===检测null
用isArray()检测Array
用instanceof检测内置对象(除Function和Array)和自定义对象
用hasOwnProperty检测属性是否在实例对象中。如果你不关心属性是在实例对象中还是在原型对象中,可以简单点用in
好了,本篇介绍如何检测JavaScript各种类型的内容就到这里了,希望大家能够认真学习本文的内容,或许对大家学习JavaScript有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]