typeof 运算符
用来判断类型的话,首先想到 typeof 运算符,具体是怎样个情况看 mdn 的解释。
typeof 操作符返回一个字符串,表示未经计算的操作数的类型。
不是很看得明白是干什么的,那我们做实验吧。
console.log(typeof 1); // number
console.log(typeof "string"); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
特殊情况
const nul = null;
const obj = { a: 1 };
const array = [1, 2, 3];
const date = new Date();
const error = new Error();
const reg = /a/g;
const func = function a() {};
console.log(typeof nul); // object
console.log(typeof obj); // object
console.log(typeof array); // object
console.log(typeof date); // object
console.log(typeof error); // object
console.log(typeof reg); // object
console.log(typeof func); // function
从上面可以知道 null 会返回 object (很奇怪),其他不管是什么对象都返回 object,但函数却能判断出来返回 function。
typeof 只能判断 6 种类型,那有没有其他办法了,可以判断更多的情况了?答案就是 Object.prototype.toString 方法。
Object.prototype.toString
先来看一下 mdn 是怎样介绍这个方法的。
每个对象都有一个
toString()
方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString()
方法被每个 Object 对象继承。如果此方法在自定义对象中未被覆盖,toString()
返回"[object type]"
,其中 type 是对象的类型。
也还是看不懂那就做实验吧。
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(null)); // [object Null]
const date = new Date();
console.log(Object.prototype.toString.call(date)); // [object Date]
结合例子很容易知道,mdn 的那段话中的 type 就是类型。
那 Object.prototype.toString 方法可以判断多少种类型了?还是做实验。
// 以下是11种:
var number = 1; // [object Number]
var string = "123"; // [object String]
var boolean = true; // [object Boolean]
var und = undefined; // [object Undefined]
var nul = null; // [object Null]
var obj = { a: 1 }; // [object Object]
var array = [1, 2, 3]; // [object Array]
var date = new Date(); // [object Date]
var error = new Error(); // [object Error]
var reg = /a/g; // [object RegExp]
var func = function a() {}; // [object Function]
function checkType() {
for (var i = 0; i < arguments.length; i++) {
console.log(Object.prototype.toString.call(arguments[i]));
}
}
checkType(number, string, boolean, und, nul, obj, array, date, error, reg, func);
还有
// 以下是2种
console.log(Object.prototype.toString.call(Math)); // [object Math]
console.log(Object.prototype.toString.call(JSON)); // [object JSON]
还有
// 以下是1种
function a() {
console.log(Object.prototype.toString.call(arguments)); // [object Arguments]
}
a();
既然有 14 种之多!!!
type API
根据上面介绍到的两种方法,我们可以写出一个 type 函数来。
// 类型映射
const classToType = {};
"Boolean Number String Function Array Date RegExp Object Error Null Undefined".split(" ").map((item, index) => {
classToType["[object " + item + "]"] = item.toLowerCase();
});
function type(obj) {
// 兼容 ie6,因为在 ie6 环境下 对 null、undefined 执行 Object.prototype.toString() 会返回 [object Object]
// null == undefined // true(神奇不)
if (obj == null) {
return obj + "";
}
return typeof obj === "object" || typeof obj === "function"
? classToType[Object.prototype.toString.call(obj)] || "object"
: typeof obj;
}
利用 type 函数封装其他
isFunction 函数
function isFunction(obj) {
return type(obj) === "function";
}
array
var isArray = Array.isArray || function( obj ) {
return type(obj) === "array";
}
参考: