面试当中经常会问到检测 js 的数据类型,我在工作当中也会用到这些方法。让我们一起走起!!!

首先给大家上一个案例

 console.log(typeof "langshen");       // String
console.log(typeof 666); // Number
console.log(typeof true); // Boolean
console.log(typeof false); // Boolean
console.log(typeof function(){}); // Function
console.log(typeof undefined); // Undefined
console.log(typeof null); // Object
console.log(typeof []); // Object
console.log(typeof {}); // Object

通过这些案例大家不难看出

第一种 : 当 typeof 检测基本数据类型(Number、String、Boolean 、Undefined、Null)的时候是没有问题的。

但是检测引用型数据类型(Array、Object、RegExp等)的时候全都是 Object。

由于 typeof 检测的数据类型不是那么的 perfect !!!会有局限性。

使用场景:

      可以判断传递的参数是否有值

 function fn(a, b) {
if (typeof b === "undefined") { // 没有传递参数
b = 0;
}
console.log(a, b); //1 0
}
fn(1);

第二种 : instanceof   检测一个实例是不是属于某个类

 console.log("langshen" instanceof String);         //false
console.log(666 instanceof Number); //false
console.log(true instanceof Boolean); //false
console.log(null instanceof Null);
console.log(undefined instanceof Undefined);
console.log([] instanceof Array); //true
console.log(function(){} instanceof Function); //true
console.log({} instanceof Object); //true

咱们先不要看 null 和 undefined

首先看基本数据类型,打印出来的都是false,这是为什么呢?

前三个都是以对象字面量创建的基本数据类型,但是却不是所属类的实例,只有实例方式创建出来的才是标准的对象数据类型值

如果我们换种方式来检测

 new String("langshen") instanceof String       //true
new Number(666) instanceof Number //true
new Boolean(true) instanceof Boolean //true

当我们通过New的方式去创建基本数据类型就输出true

所以当我们检测的数据只要在当前实例的原型链上,我们用其检测出来的结果都是true 。

另外两个特例:Null 和  Undefined 这两个没有办法比较,比较特殊

总结:

instanceof是一个操作符,返回值是一个布尔值
instanceof是检测引用数据类型,而不能检测基本数据类型

第三种: constructor

constructor这个属性存在于构造函数的原型上,指向构造函数,对象可以通过  __proto__ 在其所属类的原型上找到这个属性

 console.log(("1").constructor === String);             //true
console.log((1).constructor === Number);          //true
console.log((true).constructor === Boolean);         //true
console.log(([]).constructor === Array);            //true
console.log((function() {}).constructor === Function);    //true
console.log(({}).constructor === Object);            //true

现在看起来是不是很完美呢,其实并不是这样的,在看下面这个例子

function Fn(){};

Fn.prototype=new Array();

var f=new Fn();
console.log(f.constructor===Fn); //false
console.log(f.constructor===Array); // true

看了这个例子是不是觉得对这个世界都没有爱了!!

但我要告诉你不不不,我们要对世界保持100%热爱,由于这种热爱让我们总结出了最后一种万能的检测方法!!!

第四种:Object.prototype.toString.call()

console.log(Object.prototype.toString.call(1));              //[object Number]
console.log(Object.prototype.toString.call(''));             //[object String]
console.log(Object.prototype.toString.call(true));            //[object Boolean]
console.log(Object.prototype.toString.call(null));            // [object Null]
console.log(Object.prototype.toString.call(undefined));         //[object Undefined]
console.log(Object.prototype.toString.call([]));             // [object Array]
console.log(Object.prototype.toString.call({}));             // [object Object]
console.log(Object.prototype.toString.call(/^$/));            //[object RegExp]
console.log(Object.prototype.toString.call((function () {})));     //[object Function]

这种方法可以把多有的数据类型进行转换,是不是很神奇呢!!

想要看到更多神奇的请关注我的博客园!!!

JS中检测数据类型的多种方法的更多相关文章

  1. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  2. js中检测数据类型的几种方式

    1.typeof 一元运算符,用来检测数据类型.只可以检测number,string,boolean,object,function,undefined. 对于基本数据类型是没有问题的,但是遇到引用数 ...

  3. JS中检测数据类型的几种方式及优缺点【转】

    1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...

  4. JS中检测数据类型的几种方式及优缺点

    1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...

  5. JS中检测数据类型的四种方式及每个方式的优缺点

    //1.typeof 用来检测数据类型的运算符 //->typeof value //->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number". ...

  6. js中的数据类型及判断方法

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型. 基本类型 ● Boolean ● Null ● Undefined ● Number ● String ● Symbol (ECM ...

  7. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  8. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

随机推荐

  1. [LUOGU2730] 魔板

    搜索水题.因为只有8个数,排列一共有40320种,直接bfs,判重就行了. 但是判重的时候直接用8进制表示的话要88的bool数组.这种操作太low了,于是我们可以用康托展开,降成8!. 康托展开其实 ...

  2. [tyvj-1391]走廊泼水节 最小生成树

    做克鲁斯卡尔的时候维护一个并查集即可. #include <iostream> #include <cstdio> #include <cstring> #incl ...

  3. zabbix监控websphere的几个监控项

    首先,我要吐槽一下这个AIX系统,这该死的天杀的玩个锤子象拔蚌的系统,没有自动补齐,删除文本字符也跟linux不一样,这让用惯的linux的我各种蓝瘦. 这个问题是在项目中遇到的,由于没有接触过AIX ...

  4. Qt QImage与OpenCV Mat转换

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51029382 应一个朋友的要求,整理总 ...

  5. Callable与Futrue创建线程

    接口callable <V>  类型参数  V-call方法的结构类型 public interface Callable<V> 返回结果并且可能抛出的异常的任务.实现者定义一 ...

  6. CentOS6.5下安装远程桌面服务端软件VNC Server

    VNC 使您能够远程訪问和控制您的计算机从还有一计算机或移动设备上,不管你在世界的不论什么地方. 常见的使用情形,包含给同事和朋友提供桌面支持.远程管理您的服务器. 将 VNC Server部署到您想 ...

  7. 上机题目(中级)- 两个超级大的整数相加相减 (Java)

    代码例如以下: public class AddSub { public static void main(String[] args) { String a="46328648326846 ...

  8. ContextMenu的使用具体解释

    二话不说,先上图: 能够非常easy看到这是一个类似于Dialog悬浮在活动上的控件,它是由被注冊的view长按所触发的. 当然啦,也有其它的实现方式,这里就先介绍一下系统的ContextMenu:( ...

  9. [UVALive 6661 Equal Sum Sets] (dfs 或 dp)

    题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 15 ...

  10. cmd 进入mysql 小技巧

    1.開始中找出执行:输入cmd 2.查找appserv所在盘,我的在D盘.所以接着输入:d: 3.在d盘中查找mysql所在文件夹:cd appserv\mysql\bin 4.再输入主机名.数据库名 ...