继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JavaScript检测数据类型

为爱心太软
关注TA
已关注
手记 151
粉丝 1.4万
获赞 860

typeof 操作符

对一个值使用typeof操作符可能返回下列某个字符串:

"undefined"——如果这个值未定义;

"boolean"——如果这个值是布尔值;

"string"——如果这个值是字符串;

"number"——如果这个值是数值;

"symbol"——如果这个值是符号 (ES6 中新加入的基本类型);

"object"——如果这个值是对象或null;

"function"——如果这个值是函数。

例子:

console.log(typeof(undefined) === "undefined"); //输出:true
console.log(typeof(true) === "boolean"); //输出:true
console.log(typeof(1) === "number"); //输出:true
console.log(typeof("1") === "string"); //输出:true
console.log(typeof(Symbol()) === "symbol"); //输出:true
console.log(typeof({}) === "object"); //输出:true
console.log(typeof(null) === "object"); //输出:true
console.log(typeof(function() {}) === "function"); //输出:true

*typeof是一个操作符而不是函数,因此例子中的圆括号尽管可以使用,但不是必需的。


instanceof 操作符

如果值是给定构造函数的实例,那么 instanceof 操作符就会返回true。

例子:

console.log({} instanceof Object); //输出:true
console.log([] instanceof Array); //输出:true

注意:

1、所有引用类型的值都是 Object 构造函数的实例。因此,instanceof 操作符始终会返回true。

例子:

console.log(new Object instanceof Object) //输出:true
console.log(new Array instanceof Object) //输出:true
console.log(new Function instanceof Object) //输出:true
console.log(new Error instanceof Object) //输出:true
console.log(new Date instanceof Object) //输出:true
console.log(new RegExp instanceof Object) //输出:true
console.log(new Number instanceof Object) //输出:true
console.log(new Boolean instanceof Object) //输出:true
console.log(new String instanceof Object) //输出:true

2、使用 instanceof 操作符检测基本类型的值,则该操作符始终会返回false,因为基本类型不是对象。如果string、boolean、number变成对应的引用类型,则 instanceof 操作符会返回 true。

例子:

console.log(new String("1") instanceof Object); //输出:true
console.log(new Number(1) instanceof Object); //输出:true
console.log(new Boolean(true) instanceof Object); //输出:true

3、instanceof操作符能够正确检测引类型值的前提是单一的全局执行环境,如果存在两个以上不同的全局执行环境(多个frame或多个window),则 instanceof 操作符不能正确检测引用类型值。

例子:

<iframe></iframe>
console.log([] instanceof window.frames[0].Array);
//输出:false

4、instanceof 操作符也可以用于判断一个自定义构造函数和实例是否存在继承关系。

例子:

function Fn() {};
var newFn = new Fn();
console.log(newFn instanceof Fn);
//输出:true

Array.isArray() 方法

确定值是否是一个数组。

例子:

console.log(Array.isArray([]));
//输出:true

*支持Array.isArray()方法的浏览器有IE9+、Firefox 4+、Safari 5+、Opera 10.5+和Chrome。


Object.prototpye.toString() 方法

每个对象都有一个 toString() 方法,在任何一个对象上调用 toString() 方法都将返回一个 "[object type]"格式的字符串,其中 type 是对象的类型。利用这一点,可以安全检测引用类型值:

例子:

var toString = Object.prototype.toString;
console.log(toString.call(new Object)) // 输出:[object Object]
console.log(toString.call(new Array)) // 输出:[object Array]
console.log(toString.call(new Function)) // 输出:[objectFunction]
console.log(toString.call(new Error)) // 输出:[object Error]
console.log(toString.call(new Date)) // 输出:[object Date]
console.log(toString.call(new RegExp)) // 输出:[object RegExp]
console.log(toString.call(new Number)) // 输出:[object Number]
console.log(toString.call(new Boolean)) // 输出:[object Boolean]
console.log(toString.call(new String)) // 输出:[object String]

自定义的构造函数都将返回[object Object]。

例子:

function Fn() {};
console.log(Object.prototype.toString.call(new Fn()));
// 输出:[object Object]

基本类型值也可以使用这个方法。

例子:

console.log(Object.prototype.toString.call(null)); //输出:[object Null]
console.log(Object.prototype.toString.call(undefined)); //输出:[object Undefined]
console.log(Object.prototype.toString.call("love")); //输出:[object String]
console.log(Object.prototype.toString.call(88)); //输出:[object Number]
console.log(Object.prototype.toString.call(true)); //输出:[object Boolean]
console.log(Object.prototype.toString.call(Symbol())); //输出:[object Symbol]

文中的代码部分,带有“例子”和“测试代码”字样的,只是用来学习或测试某一功能用的代码,不可以直接用于项目的开发中。带有“代码如下”字样的,都是经过本人测试,简单修改即可用于项目开发中的代码,如有错误,欢迎指出。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP