js学习之类型识别

时间:2023-12-10 21:01:26

用来判别类型的方法有好多,整理了一下4种方法,平时用的时候,在不同情景下,还是要结合着使用的。

方法一

typeof:可以识别标准类型,除了Null;不能识别具体的对象类型,除了Function

<script>
var t = typeof(1);
console.log(t); // t==="number" var t = typeof(new Number(1))
console.log(t); // t==="object" var t = typeof("abc");
console.log(t); // t==="string" var t = typeof(new String("abc"));
console.log(t); // t==="object" var t = typeof(true);
console.log(t); // t==="boolean" var t = typeof(undefined);
console.log(t); // t==="undefined" var t = typeof(null);
console.log(t); // t==="object" var t = typeof({});
console.log(t); // t==="object" var t = typeof([]);
console.log(t); // t==="object" var t = typeof(new Date);
console.log(t); // t==="object" var t = typeof(/\d/);
console.log(t); // t==="object" var t = typeof(function(){});
console.log(t); // t==="function"
</script>

方法二

instanceof:可以识别内置对象类型,不能识别原始类型,可以识别自定义对象类型

<script>
var t = new Number(1);
console.log(1 instanceof Number); // false
console.log(1 instanceof Object); // false
console.log(t instanceof Number); // true
console.log(t instanceof Object); // true var t = new String("abc");
console.log("abc" instanceof String); // false
console.log("abc" instanceof Object); // false
console.log(t instanceof String); // true
console.log(t instanceof Object); // true console.log(true instanceof Boolean); // false
console.log(true instanceof Object); // false console.log(undefined instanceof Object); // false
console.log(undefined instanceof Undefined); // Uncaught ReferenceError: Undefined is not defined console.log(null instanceof Object); // false
console.log(null instanceof Null); // Uncaught ReferenceError: Undefined is not defined var t = new Object({});
console.log(t instanceof Object); // true
console.log({} instanceof Object); // true var t = new Array([]);
console.log(t instanceof Array); // true
console.log(t instanceof Object); // true
console.log([] instanceof Array); // true
console.log([] instanceof Object); // true var t = new Date();
console.log(t instanceof Date); // true
console.log(t instanceof Object); // true console.log(/\d/ instanceof Object); // true
console.log(/\d/ instanceof Regexp); // Uncaught ReferenceError: Undefined is not defined var t = function(){};
console.log(t instanceof Function); // true
console.log(t instanceof Object); // true
</script>

方法三:可以识别标准类型以及内置对象类型,不能识别自定义类型

    function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1);
}

示例:

<script>
var t = type(1);
console.log(t); // t==="Number" var t = type(new Number(1));
console.log(t); // t==="Number" var t = type("abc");
console.log(t); // t==="String" var t = type(new String("abc"));
console.log(t); // t==="String" var t = type(true);
console.log(t); // t==="Boolean" var t = type(undefined);
console.log(t); // t==="Undefined" var t = type(null);
console.log(t); // t==="Null" var t = type({});
console.log(t); // t==="Object" var t = type([]);
console.log(t); // t==="Array" var t = type(new Date);
console.log(t); // t==="Date" var t = type(/\d/);
console.log(t); // t==="Regexp" var t = type(function(){});
console.log(t); // t==="Function" function add(){};
var f = new add();
var t = type(f);
console.log(t); // t==="Object"
</script>

方法四:可以识别所有类型:标准类型、内置对象类型、自定义对象类型

        function getConstructorName(obj){
return (obj===undefined||obj===null)?obj:(obj.constructor&&obj.constructor.toString().match(/function\s*([^(]*)/)[1]);
}

示例:

<script>
var t = getConstructorName(1);
console.log(t); // t==="Number" var t = getConstructorName(new Number(1));
console.log(t); // t==="Number" var t = getConstructorName("abc");
console.log(t); // t==="String" var t = getConstructorName(new String("abc"));
console.log(t); // t==="String" var t = getConstructorName(true);
console.log(t); // t==="Boolean" var t = getConstructorName(undefined);
console.log(t); // t==="undefined" var t = getConstructorName(null);
console.log(t); // t==="null" var t = getConstructorName({});
console.log(t); // t==="Object" var t = getConstructorName([]);
console.log(t); // t==="Array" var t = getConstructorName(new Date);
console.log(t); // t==="Date" var t = getConstructorName(/\d/);
console.log(t); // t==="Regexp" var t = getConstructorName(function(){});
console.log(t); // t==="Function" function add(){};
var f = new add();
var t = getConstructorName(f);
console.log(t); // t==="add"
</script>

Demo:

输入任意格式的年月日,均返回 Date 格式:

/*
* 输入格式:
* '2016-11-06'
* 1478401733333
* {y:2016,m:11,d:6}
* [2016,11,6]
* 返回格式:Date
*/
function toDate(param){
if (typeof(param) == 'string' ||
typeof(param) == 'number' ){
return new Date(param);
}
if (param instanceof Array){
var date = new Date(0);
date.setYear(param[0]);
date.setMonth(param[1]-1);
date.setDate(param[2]);
return date;
}
if (typeof(param) == 'object') {
var date = new Date(0);
date.setYear(param.y);
date.setMonth(param.m-1);
date.setDate(param.d);
return date;
}
return -1;
}