首页 技术 正文
技术 2022年11月6日
0 收藏 595 点赞 634 浏览 4274 个字

用来判别类型的方法有好多,整理了一下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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849