js学习之类型识别
用来判别类型的方法有好多,整理了一下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;
}
js学习之类型识别的更多相关文章
- JS中的类型识别
JS为弱类型语言,所以类型识别对JS而言尤为重要,JS中常用的类型识别方法有4种:typeof.Object.prototype.toString.constructor和instanceof. (1 ...
- 关于js中的类型内容总结(类型识别)
JS 有7种数据类型: 6种原始类型:Boollean String Number Null Underfined Symbol 引用类型:Object 类型识别主要有以下四 ...
- JS学习笔记(一)基本数据类型和对象类型
js是一种弱类型的语言,所有的变量都用var进行声明,字符串用双引号或单引号括起来,常见基本数据类型为number,string,boolean等.如 var num = 123;或var num = ...
- WebGL three.js学习笔记 6种类型的纹理介绍及应用
WebGL three.js学习笔记 6种类型的纹理介绍及应用 本文所使用到的demo演示: 高光贴图Demo演示 反光效果Demo演示(因为是加载的模型,所以速度会慢) (一)普通纹理 计算机图形学 ...
- JS 学习笔记--8---Function类型
练习使用的浏览器IE11 JS 中Function类型实际上是一种对象,每一个函数实际上都是Function类型的一个实例,每一个函数都有一些默认的属性和方法.由于函数是对象,故函数名实际上也是一 ...
- (C/C++学习笔记) 二十三. 运行时类型识别
二十三. 运行时类型识别 ● 定义 运行时类型识别(Run-time Type Identification, RTTI) 通过RTTI, 程序能够使用基类的指针或引用来检查(check)这些指针或引 ...
- C++学习之显式类型转换与运行时类型识别RTTI
static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...
- Javascript 基础学习(三)js 的原始类型和声明变量
java的基本数据类型一共有 byte short int long float double char boolean js中定义变量使用关键字 var js的原始类型(五个) String: 字符 ...
- Ext JS学习第十六天 事件机制event(一) DotNet进阶系列(持续更新) 第一节:.Net版基于WebSocket的聊天室样例 第十五节:深入理解async和await的作用及各种适用场景和用法 第十五节:深入理解async和await的作用及各种适用场景和用法 前端自动化准备和详细配置(NVM、NPM/CNPM、NodeJs、NRM、WebPack、Gulp/Grunt、G
code&monkey Ext JS学习第十六天 事件机制event(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...
随机推荐
- ASP.NET Core应用的错误处理[2]:DeveloperExceptionPageMiddleware中间件如何呈现“开发者异常页面”
在<ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式>中,我们通过几个简单的实例演示了如何呈现一个错误页面,这些错误页面的呈现分别由三个对应的中间件来完成,接下来我们将 ...
- C# 注册 Windows 热键
闲扯: 前几日,一个朋友问我如何实现按 F1 键实现粘贴(Ctrl+V)功能,百度了一个方法,发给他,他看不懂(已经是 Boss 的曾经的码农),我就做了个Demo给他参考.今日得空,将 Demo 整 ...
- linux基础学习笔记
我用的是centOS7.0版本的系统.linux的shell终端窗口类似于wind的command窗口 shell命令提示符格式:用户名@主机名:目录名 提示符 @前面的是已登录的用户名,@之后的为计 ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- System.FormatException: GUID 应包含带 4 个短划线的 32 位数(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。
在NHibernate数据库查询中出现了这个错误,由于是数据库是mysql的,当定义的字段为char(36)的时候就会出现这个错误. [解决方法] 将char(36) 改成varchar(40)就行了 ...
- ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式
由于ASP.NET Core应用是一个同时处理多个请求的服务器应用,所以在处理某个请求过程中抛出的异常并不会导致整个应用的终止.出于安全方面的考量,为了避免敏感信息的外泄,客户端在默认的情况下并不会得 ...
- 利用apply()或者rest参数来实现用数组传递函数参数
关于call()和apply()的用法,MDN文档里写的非常清晰明白,在这里就不多做记录了. https://developer.mozilla.org/zh-CN/docs/Web/JavaScri ...
- 使用EF CodeFirst 创建数据库
EntityFramework 在VS2015添加新建项时,选择数据->ADO.NET 实体数据模型,有一下选项 来自数据库的EF设计器,这个就是我们最常用的EntityFramework设计模 ...
- BPM配置故事之案例14-数据字典与数据联动
小明遇到了点麻烦,他昨天又收到了行政主管发来的邮件,要求把出差申请单改由H3 BPM进行,表单如下 行政主管的出差申请表 小明对表单进行了调整,设计出了一份适合在系统中使用的表单,但在"出差 ...
- Android Studio 编译单个module
前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiru ...