类型判断
typeof(type);
原生
'undefined'
*'boolean'
'string'
'number'
'object'
'function'
/*
* 类型检测:
*
* instanceof 操作符可以用来判断某个构造函数的 prototype 属性是否存在另外一个要检测对象的原型链上。
* 也就是判断instanceof前面的对象是否是后面的类或对象的实例。
*
*
*
* */
/*
* Object.prototype.toString()
* 在Object基本类定义的这个toString()方法,是用来检测数据类型的;
* */
console.log(Object.prototype.toString.call(1));//[object Number]
console.log(Object.prototype.toString.call(''));//[object String]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(null));// [object Null]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call([]));// [object Array]
console.log(Object.prototype.toString.call({}));// [object Object]
console.log(Object.prototype.toString.call(/^$/));//[object RegExp]
console.log(Object.prototype.toString.call((function () {})));//[object Function]
自定义类型判断函数
function typeOf(obj) {
var typeList = {
'[object Boolean]' : 'boolean',
'[object Number]' : 'number',
'[object String]' : 'string',
'[object Function]' : 'function',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object RegExp]' : 'regExp',
'[object Undefined]': 'undefined',
'[object Null]' : 'null',
'[object Object]' : 'object'
};
return typeList [Object.prototype.toString.call(obj)];
}