js数据类型检测(编写一个函数,能识别各种类型,包括RegExp,Set,Null等)

JavaScript中的数据类型

1.基本数据类型

Undefined、Null、Boolean、Number、String、Symbol(ES6新增,表示第一无二的值)
特点:
(1)值不可变
(2)存放在栈中
(3)双等和全等的区分

2.引用类型

包括Object、Array、Funciton、Date等
特点:
(1)值可变
(2)同时保存在栈内存和堆内存
(3)比较是引用的比较

Javascript数据类型的检测

  1. typeof:返回一个表示数据类型的字符串(Undefined、Boolean、Number、String、Symbol、Object、Function)
    不能判断Null、Array、Date、RegExp
typeof Symbol(); // symbol 有效
typeof ''; // string 有效
typeof 1; // number 有效
typeof true; //boolean 有效
typeof undefined; //undefined 有效
typeof new Function(); // function 有效
typeof new Object();// object 有效
typeof null; //object 无效
typeof [] ; //object 无效
typeof new Date(); //object 无效
typeof new RegExp(); //object 无效

2.instanceof:判断A是否是B的实例(A instanceof B),返回布尔值,便是一个对象在其原型链中是否存在一个构造函数的prototype属性
缺点:
(1)字面量创建和实例方式创建有区别,只有实例创建的结果才是标准的。

console.log(1 instanceof Number)//false
console.log(new Number(1) instanceof Number)//true

(2)只要在当前的实例原型链上,检测结果都为true。

var arr = [1, 2, 3];
console.log(arr instanceof Array) // true
console.log(arr instanceof Object);  // true
function fn(){}
console.log(fn instanceof Function)// true
console.log(fn instanceof Object)// true

(3)不能检测 null 和 undefined: 对于特殊的数据类型 null 和 undefined,他们的所属类是 Null 和 Undefined,但是浏览器把这两个类保护起来了,不允许我们在外面访问使用。

使用instanceof检测 数组、对象、时间对象、正则对象
[] instanceof Array; //true
{} instanceof Object;//true
new Date() instanceof Date;//true
new RegExp() instanceof RegExp//true

3.constructor: constructor 检测 Object 与 instanceof 不一样,还可以处理基本数据类型的检测。

var aa=[1,2];
console.log(aa.constructor===Array);//true
console.log(aa.constructor===RegExp);//false
console.log((1).constructor===Number);//true
var reg=/^$/;
console.log(reg.constructor===RegExp);//true
console.log(reg.constructor===Object);//false

缺点:
(1) null 和 undefined 是无效的对象,因此是不会有 constructor 存在的
(2)函数可以把类的原型进行重写,所以检测不稳定
4.Object.prototype.toString.call() : Object.prototype.toString.call() 最准确最常用的方式。首先获取 Object 原型上的 toString 方法,让方法执行,让 toString 方法中的 this 指向第一个参数的值。
在这里插入图片描述
(1)Object对象和它的原型链上各自有一个toString()方法,第一个返回的是一个函数,第二个返回的是值类型。
(2)一般情况下,js中对象的toString(),返回字符串,内容与函数声明语法有关,例如[1,2,3].toString()//“1,2,3”
(3)大多数都返回函数的完整源码,Array.toString()//“function Array() { [native code] }”
(4)内置函数往往返回一个类似"[native code]“的函数体,需要配合call方法,比如Object.prototype.toString.call([1,2,3])//”[object Array]"

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window是全局对象global的引用

使用js编写一个函数getType,能识别各种类型,包括RegExp,Set,Null等

function getType(target) {
	var type = Object.prototype.toString.call(target);
	//根据空格切分
    var list = type.split(/[ ]/);
    //获取第二个字符串,然后再进行以“]”分隔符切分
    var newType = list[1].split("]")
    //获取第一个字符串
    // console.log(newType[0])
    return newType[0];
}
console.log(getType(Symbol())); // Symbol有效 
console.log(getType('')); // String有效
console.log(getType(1)); // Number有效
console.log(getType(true)); //Boolean有效
console.log(getType(undefined)); //Undefined有效
console.log(getType(new Function())); // Function有效
console.log(getType(null)); //Null 有效
console.log(getType([])); //Array 有效
console.log(getType(new Date())); //Date 有效
console.log(getType(new RegExp())); //RegExp 有效
console.log(getType(new Set())); //Set有效

由于今天做到了一个笔试题,不是很懂,只想到了用typeof,但是又不能判断Null、RegExp、Set类型,没有经常使用Object.prototype.toString.call(),没有考虑到,现在参考了一个博主的文章,有了一点思路,就把它记录写来了

参考文章:https://www.cnblogs.com/jiaoshou/p/13720093.html

越努力越幸运!!!!
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值