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(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...
随机推荐
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- Git与Repo入门
版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工程等的修改历史,方便查看更改历史,备份以便恢复以前的版本,多人协作... 一.原始版本控制 最原始的版本控制是纯手工的版本控制:修改文 ...
- ABP文档 - Hangfire 集成
文档目录 本节内容: 简介 集成 Hangfire 面板授权 简介 Hangfire是一个综合的后台作业管理器,可以在ABP里集成它替代默认的后台作业管理器,你可以为Hangfire使用相同的后台作业 ...
- .Net多线程编程—预备知识
1 基本概念 共享内存的多核架构:一个单独的封装包内封装了多个互相连接的未处理器,且所有内核都可以访问主内存.共享内存的多核系统的一些微架构,例如内核暂停功能,超频. 内核暂停功能:当使用内核不多的时 ...
- ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件
虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- android键盘
在应用的开发过程中有不少的情况下会用到自定义键盘,例如支付宝的支付密码的输入,以及类似的场景.android系统给开发者们提供了系统键盘,KeyboardView,其实并不复杂,只是有些开发者不知道罢 ...
- pt-mext
pt-mext实现的功能比较简单,就是将mysqladmin输出的多次迭代的相同status变量值放到同一行输出. 参数很少,除了--help和--version外,只有一个--relative参数 ...
- 写出易调试的SQL
h4 { background: #698B22 !important; color: #FFFFFF; font-family: "微软雅黑", "宋体", ...
- jq选择器基础
Jquery $代表选择器 使用jq必须要导入jq文件 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js&qu ...