Object.prototype.toString.call(value)

使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下:

Object.prototype.toString.call(value)

1.判断基本类型:

1
2
3
4
5
Object.prototype.toString.call(null);//”[object Null]”
Object.prototype.toString.call(undefined);//”[object Undefined]”
Object.prototype.toString.call(“abc”);//”[object String]”
Object.prototype.toString.call(123);//”[object Number]”
Object.prototype.toString.call(true);//”[object Boolean]”

2.判断原生引用类型:

函数类型

1
2
Function fn(){console.log(“test”);}
Object.prototype.toString.call(fn);//”[object Function]”

日期类型

1
2
var date = new Date();
Object.prototype.toString.call(date);//”[object Date]”

数组类型

1
2
var arr = [1,2,3];
Object.prototype.toString.call(arr);//”[object Array]”

正则表达式

1
2
var reg = /[hbc]at/gi;
Object.prototype.toString.call(reg);//”[object RegExp]”

自定义类型

1
2
3
4
5
6
function Person(name, age) {
this.name = name;
this.age = age;
}
var person = new Person("Rose", 18);
Object.prototype.toString.call(arr); //”[object Object]”

很明显这种方法不能准确判断person是Person类的实例,而只能用instanceof 操作符来进行判断,如下所示:

1
console.log(person instanceof Person);//输出结果为true

3.判断原生JSON对象:

1
2
var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);
console.log(isNativeJSON);//输出结果为”[object JSON]”说明JSON是原生的,否则不是;

注意:Object.prototype.toString()本身是允许被修改的,而我们目前所讨论的关于Object.prototype.toString()这个方法的应用都是假设toString()方法未被修改为前提的。

1
2
3
4
5
6
7
8
9
10
11
12
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]

(无法区分自定义对象类型,自定义类型可以采用instanceof区分)

  1. JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。

    对于数组、函数、对象来说,其关系复杂,若使用 typeof 都会统一返回 为object,这样为后续处理带来不便。

  2. 关键函数方法

    js原生方法Object.prototype.toString.call();它可以给出数据的确切类型,相比typeof要精确。
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function getDataType(data){
//Object.prototype.toString.call(data).splice(8,-1)
var getType=Object.prototype.toString;
var myType=getType.call(data);//调用call方法判断类型,结果返回形如[object Function]
var typeName=myType.slice(8,-1);//[object Function],即取除了“[object ”的字符串。
var copyData='';//复制后的数据
console.log(data+" is "+typeName);
return copyData;
}
getDataType(123);
getDataType("123");
getDataType(null);
getDataType(undefined);
getDataType(false);
getDataType([1,2,4]);
getDataType({"name":"wc"});
getDataType(function(){alert(23);});

// 123 is Number
// 123 is String
// null is Null
// undefined is Undefined
// false is Boolean
// 1,2,4 is Array
// [object Object] is Object
// function (){alert(23);} is Function
感谢你的打赏哦!