如何判断JavaScript数据具体类型
昨晚做了阿里的题目,让我写一个函数,可以判断数据的具体类型。其实题目很简单的。。。但是真的是自己不太注意吧,写的很糟糕啊。
然后今天就自己写了一个,然后又到晚上搜了下,看看别人的写法,结果发现自己有点思维受限啊,不够开阔啊,那些方法其实都是见过的,可能是自己没有梳理过,或者认真对待。今天就把这些方法整理一下。
1、基本数据类型采用typeof,这个返回的都是很准的。
var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var e = function(){alert(111);};
var f = function(){this.name="22";}; alert(typeof a) ------------> string
alert(typeof b) ------------> number
alert(typeof c) ------------> object//数组是引用类型的
alert(typeof f) ------------> function
alert(typeof e) ------------> function
2、引用类型采用instanceof, 主要针对的是采用new 实例化的对象。
var a = new String("iamstring.");
var b = new Number(222);
var c = new Array(222,2,4);
var e = function(){console.log(111);};
var f = function(){this.name="22";};
var f = function(){this.name="22";};
var h = new Error("foo");
console.log(a instanceof String) //true
console.log(b instanceof Number) //true
console.log(c instanceof Array) //true
console.log(e instanceof Function) //true
console.log(f instanceof Function) //true
console.log(h instanceof Error) //true
3、根据constructor来判断
其中a-h跟上面的一样,就不重复定义了,注意不带引号的,是要大写
var i="str", arr=[1], num=1;
console.log(a.constructor=== String) //true
console.log(b.constructor=== Number) //true
console.log(c.constructor=== Array) //true
console.log(e.constructor=== Function) //true
console.log(f.constructor=== Function) //true
console.log(h.constructor===Error) //true
console.log(i.constructor===String) //true
console.log(num.constructor=== Number) //true
console.log(arr.constructor=== Array) //true
4、toString()方法,这是最通用的,大小写不能写错,有点麻烦
console.log(Object.prototype.toString.call(a) === ‘[object String]’) -------> true; console.log(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true; console.log(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true; console.log(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true; console.log(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true; console.log(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;
5、可以采用一些某些数据类型采用的方法
比如:string 类型的 replace
regexp类型的test, exec
数组还有一种判断方法:Array.isArray()
date 类型的 getMonth()等
如何判断JavaScript数据具体类型的更多相关文章
- Token验证的流程及如何准确的判断一个数据的类型
Token验证的流程: 1,客户端使用用户名跟密码请求登录:2,服务端收到请求,去验证用户名与密码:3,验证成功后,服务端会签发一个 Token,再把这个 Token 发送给客户端:4,客户端收到 T ...
- javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)
function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...
- js获取或判断任意数据类类型的通用方法(getDataType)和将NodeList转为数组(NodeListToArray)
function getDataType(any){ /* (1) Object.prototype.toString.call 方法判断类型: 优点:通用,返回"[object Strin ...
- 判断javaScript变量是Ojbect类型还是Array类型
JavaScript是弱类型的语言,所以对变量的类型并没有强制控制类型.所以声明的变量可能会成为其他类型的变量, 所以在使用中经常会去判断变量的实际类型. 对于一般的变量我们会使用typeof来判 ...
- [Javascript]js判断是否为undefined类型
概述 在项目获取某个元素的值会出现undefined,所以对这种情况要有特殊处理. 可通过下面的代码判断是否为undefined类型. if (typeof(reValue) == "und ...
- Javascript学习1 - Javascript中的类型对象
原文:Javascript学习1 - Javascript中的类型对象 1.1关于Numbers对象. 常用的方法:number.toString() 不用具体介绍,把数字转换为字符串,相应的还有一个 ...
- javascript 中的类型
javascript 中的类型 js 是一门弱语言,各式各样的错误多种多样,特别是确定返回值有问题的时候,你会用什么来进行表示错误? 我一般有三个选择: null '' error {} 第一个选择 ...
- javascript学习-基本类型
javascript学习-基本类型 1.概述 javascript的数据类型大体上分两种:基本类型和对象类型.简单的区分就是基本类型是无法再分的原子级类型:对象类型是容器,可以容纳基本类型和对象类型. ...
- JavaScript数据检测
前言: 随着编程实践的增加,慢慢发现关于数据类型的检测至关重要.我认为程序就是为了处理数据和展示数据.所以,数据的检测对于编程来说也至关重要.因为只有符合我们预期的输入,才可能产生正确的输出.众所周知 ...
随机推荐
- express 错误处理
原谅我的无知,之前学习express时,没想过需要错误处理.app.js也没认真看. 现在做具体的项目时,需要考虑到出错的情况. 其实有两种: 1.nodejs是单线程,如果挂掉了,网站就会崩溃,需要 ...
- 未能加载文件或程序集“System.Web.Http.WebHost, Version=4.0.0.0, ”或它的某一个依赖项。系统找不到指定的文件。
第一次发布MVC项目,打开网站 未能加载文件或程序集“System.Web.Http.WebHost, Version=4.0.0.0, ”或它的某一个依赖项.系统找不到指定的文件. 问题原因:缺少配 ...
- AEAI Portal 权限体系说明
1.概述 在数通畅联的产品体系中,AEAI Portal毫无疑问的占据了很重要的地位,在这里我们将通过参考Portal样例,讲述一下AEAI Portal权限体系的控制方法.在Portal使用过程中, ...
- Android开发教程 - 使用Data Binding(二)集成与配置
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- 【算法python实现】 -- 不同路径
原题:https://leetcode-cn.com/problems/unique-paths/ 问题描述 n行m列的表格,从a[0][0]出发,每次只能右移一步或者下移一步,求到a[n-1][m- ...
- ubuntu14中把openssh升级到7.5
经验证可用 下载相关tar.gz包,没有下载到的,可以在这里下载.http://download.csdn.net/download/songanshu/10023663 apt install -y ...
- JS实现网页背景旋转缩放轮播效果
实现效果:效果预览 css代码: .switch_images { display: inline-block; margin:; padding:; width: 100%; height: 100 ...
- iOS-自定义NavigationItem返回按钮【pop返回按钮】
在用navigationVC时,返回按钮有时候不想用系统的,这里用继承的方式把按钮替换了,同时也可以实现系统的右滑返回,很简单: 1.创建基类 BasePopViewController 创建一个用于 ...
- 使用bash echo 输出回车转义
输出回车 [root@~]# echo -e 'hello\n'hello 回车去掉 [root@~]# echo -n hello hello[root@~]#
- jdk1.8一键安装脚本(linux环境)
1.下载jdk安装包和安装脚本 下载地址:https://pan.baidu.com/s/1bo6ADQ3 其中包括: jdk安装包:jdk-8u151-linux-x64.tar.gz jdk一键安 ...