javascript中检测一个变量的类型

 1     /**
 2      * 怎么检测一个变量的类型?
 3      * 在js中检测对象类型主要有三种:typeof, instanceof, constructor, 这几种都可以检测对象的类型.
 4      * 另外还可以适应jQuery来检测类型.
 5      * */
 6 
 7 
 8     /**
 9      * 1.使用typeof检测对象类型
10      * typeof作为最常用的检测类型方法,返回字符串类型;
11      * */
12     function testType(value) {
13         var str = typeof(value);
14         switch(str){
15             case 'undefined': //undefined类型
16                 console.log(str);
17                 break;
18             case 'object': //null类型, 任意内置对象, 数组
19                 console.log(str);
20                 break;
21             case 'boolean': //true, false类型
22                 console.log(str);
23                 break;
24             case 'string': //string字符串类型
25                 console.log(str);
26                 break;
27             case 'function': //任意函数
28                 console.log(str);
29                 break;
30             case 'number'://任意的数值类型,包含NaN
31                 console.log(str);
32                 break;
33         }
34     }
35     testType(111);
36     /**
37      * 可以看出typeof对于基本类型可以测试出类型,但是对于其他的,包括日期, 数组等等大多都返回object类型,而且null也返回的是object类型,也就是没有办法知道确切类型.
38      * */

另一种改进的检测方法, 是使用默认的toString(), 继承自Object,可以返回类型信息.

1     function classof(o){
2         if(o === null) return "Null";
3         if(o === undefined) return "Undefined";
4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
5     }

那为什么不直接用 a.toString() 而要用 Object.prototype.toString.call(a) ?

如果是数组或者函数,结果会是怎么样?

原因是:直接使用toString()有可能这个方法被重写了, 而不是默认的. 使用Object.prototype.toString().call(o).slice(8,-1);得到的结果更加可靠!

完整代码:

 1 function classof(o){
 2         if(o === null) return "Null";
 3         if(o === undefined) return "Undefined";
 4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
 5     }
 6     function testType(value) {
 7         var str = classof(value);
 8         console.log(str);
 9         switch(str){
10             case 'undefined': //undefined类型
11                 console.log(str);
12                 break;
13             case 'object': //null类型, 任意内置对象, 数组
14                 console.log(str);
15                 break;
16             case 'boolean': //true, false类型
17                 console.log(str);
18                 break;
19             case 'string': //string字符串类型
20                 console.log(str);
21                 break;
22             case 'function': //任意函数
23                 console.log(str);
24                 break;
25             case 'number'://任意的数值类型,包含NaN
26                 console.log(str);
27                 break;
28             case 'Date': //日期
29                 console.log(str);
30                 break;
31             case 'Array': //数组
32                 console.log(str);
33                 break;
34             case 'RegExp': //正则
35                 console.log(str);
36                 break;
37         }
38     }
39     //testType(function(){console.log(this)});//function
40     testType();

可以看到改进了一部分

2.使用instanceof检测对象类型

对于typeof()检测为Object类型的可以使用instanceof进一步检测具体类型.instanceof实际上检测的是对象的原型.

可以检测变量是不是某个对象的实例,返回bool值.

1 var value = new Date();
2 var isDate = value instanceof Date;
3 console.log(isDate);//true

3.使用constructor检测对象类型

constructor相当于检测构造函数,返回的是一个函数.

 1     function testconstructor(value) {
 2         var str = value.constructor;
 3         console.log(value.constructor);
 4         switch(str){
 5             case Number:
 6                 console.log(str);
 7                 break;
 8         }
 9     }
10     testconstructor(111); //function Number()

如果需要检测一个对象的确切类型,可以综合使用这三种方法:

 1 function type(o) {
 2         var t, c, n;//type class name
 3         //null类型
 4         if(o === null) return "null";
 5         //是数值中的特殊类型:NaN
 6         if(o !== o) return "nan";
 7         //使用typeof检测除去 "object" 类型为的其他类型.
 8         if((t = typeof o) !== "object") return t;
 9         //typeof检测为"object"类型,则进一步检测
10         //可以检测出大部分内置类型
11         if((c = classof(o)) !== "Object") return c;
12         //classof(o)返回为"Object"时,检测constructor
13         if(o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n;
14         //无法识别的其他类型,默认为"Object"
15         return "Object";
16     }
17     function classof(o) {
18         return Object.prototype.toString.call(o).slice(8,-1);
19     };
20 
21     //返回function的名称, 可能为""或者 null
22     Function.prototype.getName = function() {
23         if ("name" in this) return this.name;
24         return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
25     };
26     type(NaN);

还可以用jQuery来检测类型,常用的方法如下:

jQuery.isArray(obj);//检测对象是否是数组

1     jQuery.isFunction(obj);//测试对象是否为函数。
2     jQuery.isEmptyObject(obj);// jQuery 1.4 中,这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
3     jQuery.isPlainObject(obj);// 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
4     jQuery.isWindow(obj);// 测试对象是否是窗口(有可能是Frame)。
5     jQuery.type(obj);// 检测obj的数据类型。
6     jQuery.isNumeric(value);// 确定它的参数是否是一个数字,包含16进制数

---恢复内容结束---

 1     /**
 2      * 怎么检测一个变量的类型?
 3      * 在js中检测对象类型主要有三种:typeof, instanceof, constructor, 这几种都可以检测对象的类型.
 4      * 另外还可以适应jQuery来检测类型.
 5      * */
 6 
 7 
 8     /**
 9      * 1.使用typeof检测对象类型
10      * typeof作为最常用的检测类型方法,返回字符串类型;
11      * */
12     function testType(value) {
13         var str = typeof(value);
14         switch(str){
15             case 'undefined': //undefined类型
16                 console.log(str);
17                 break;
18             case 'object': //null类型, 任意内置对象, 数组
19                 console.log(str);
20                 break;
21             case 'boolean': //true, false类型
22                 console.log(str);
23                 break;
24             case 'string': //string字符串类型
25                 console.log(str);
26                 break;
27             case 'function': //任意函数
28                 console.log(str);
29                 break;
30             case 'number'://任意的数值类型,包含NaN
31                 console.log(str);
32                 break;
33         }
34     }
35     testType(111);
36     /**
37      * 可以看出typeof对于基本类型可以测试出类型,但是对于其他的,包括日期, 数组等等大多都返回object类型,而且null也返回的是object类型,也就是没有办法知道确切类型.
38      * */

另一种改进的检测方法, 是使用默认的toString(), 继承自Object,可以返回类型信息.

1     function classof(o){
2         if(o === null) return "Null";
3         if(o === undefined) return "Undefined";
4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
5     }

那为什么不直接用 a.toString() 而要用 Object.prototype.toString.call(a) ?

如果是数组或者函数,结果会是怎么样?

原因是:直接使用toString()有可能这个方法被重写了, 而不是默认的. 使用Object.prototype.toString().call(o).slice(8,-1);得到的结果更加可靠!

完整代码:

 1 function classof(o){
 2         if(o === null) return "Null";
 3         if(o === undefined) return "Undefined";
 4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
 5     }
 6     function testType(value) {
 7         var str = classof(value);
 8         console.log(str);
 9         switch(str){
10             case 'undefined': //undefined类型
11                 console.log(str);
12                 break;
13             case 'object': //null类型, 任意内置对象, 数组
14                 console.log(str);
15                 break;
16             case 'boolean': //true, false类型
17                 console.log(str);
18                 break;
19             case 'string': //string字符串类型
20                 console.log(str);
21                 break;
22             case 'function': //任意函数
23                 console.log(str);
24                 break;
25             case 'number'://任意的数值类型,包含NaN
26                 console.log(str);
27                 break;
28             case 'Date': //日期
29                 console.log(str);
30                 break;
31             case 'Array': //数组
32                 console.log(str);
33                 break;
34             case 'RegExp': //正则
35                 console.log(str);
36                 break;
37         }
38     }
39     //testType(function(){console.log(this)});//function
40     testType();

可以看到改进了一部分

2.使用instanceof检测对象类型

对于typeof()检测为Object类型的可以使用instanceof进一步检测具体类型.instanceof实际上检测的是对象的原型.

可以检测变量是不是某个对象的实例,返回bool值.

1 var value = new Date();
2 var isDate = value instanceof Date;
3 console.log(isDate);//true

3.使用constructor检测对象类型

constructor相当于检测构造函数,返回的是一个函数.

 1     function testconstructor(value) {
 2         var str = value.constructor;
 3         console.log(value.constructor);
 4         switch(str){
 5             case Number:
 6                 console.log(str);
 7                 break;
 8         }
 9     }
10     testconstructor(111); //function Number()

如果需要检测一个对象的确切类型,可以综合使用这三种方法:

 1 function type(o) {
 2         var t, c, n;//type class name
 3         //null类型
 4         if(o === null) return "null";
 5         //是数值中的特殊类型:NaN
 6         if(o !== o) return "nan";
 7         //使用typeof检测除去 "object" 类型为的其他类型.
 8         if((t = typeof o) !== "object") return t;
 9         //typeof检测为"object"类型,则进一步检测
10         //可以检测出大部分内置类型
11         if((c = classof(o)) !== "Object") return c;
12         //classof(o)返回为"Object"时,检测constructor
13         if(o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n;
14         //无法识别的其他类型,默认为"Object"
15         return "Object";
16     }
17     function classof(o) {
18         return Object.prototype.toString.call(o).slice(8,-1);
19     };
20 
21     //返回function的名称, 可能为""或者 null
22     Function.prototype.getName = function() {
23         if ("name" in this) return this.name;
24         return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
25     };
26     type(NaN);

还可以用jQuery来检测类型,常用的方法如下:

jQuery.isArray(obj);//检测对象是否是数组

1     jQuery.isFunction(obj);//测试对象是否为函数。
2     jQuery.isEmptyObject(obj);// jQuery 1.4 中,这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
3     jQuery.isPlainObject(obj);// 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
4     jQuery.isWindow(obj);// 测试对象是否是窗口(有可能是Frame)。
5     jQuery.type(obj);// 检测obj的数据类型。
6     jQuery.isNumeric(value);// 确定它的参数是否是一个数字,包含16进制数

---恢复内容开始---

 1     /**
 2      * 怎么检测一个变量的类型?
 3      * 在js中检测对象类型主要有三种:typeof, instanceof, constructor, 这几种都可以检测对象的类型.
 4      * 另外还可以适应jQuery来检测类型.
 5      * */
 6 
 7 
 8     /**
 9      * 1.使用typeof检测对象类型
10      * typeof作为最常用的检测类型方法,返回字符串类型;
11      * */
12     function testType(value) {
13         var str = typeof(value);
14         switch(str){
15             case 'undefined': //undefined类型
16                 console.log(str);
17                 break;
18             case 'object': //null类型, 任意内置对象, 数组
19                 console.log(str);
20                 break;
21             case 'boolean': //true, false类型
22                 console.log(str);
23                 break;
24             case 'string': //string字符串类型
25                 console.log(str);
26                 break;
27             case 'function': //任意函数
28                 console.log(str);
29                 break;
30             case 'number'://任意的数值类型,包含NaN
31                 console.log(str);
32                 break;
33         }
34     }
35     testType(111);
36     /**
37      * 可以看出typeof对于基本类型可以测试出类型,但是对于其他的,包括日期, 数组等等大多都返回object类型,而且null也返回的是object类型,也就是没有办法知道确切类型.
38      * */

另一种改进的检测方法, 是使用默认的toString(), 继承自Object,可以返回类型信息.

1     function classof(o){
2         if(o === null) return "Null";
3         if(o === undefined) return "Undefined";
4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
5     }

那为什么不直接用 a.toString() 而要用 Object.prototype.toString.call(a) ?

如果是数组或者函数,结果会是怎么样?

原因是:直接使用toString()有可能这个方法被重写了, 而不是默认的. 使用Object.prototype.toString().call(o).slice(8,-1);得到的结果更加可靠!

完整代码:

 1 function classof(o){
 2         if(o === null) return "Null";
 3         if(o === undefined) return "Undefined";
 4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
 5     }
 6     function testType(value) {
 7         var str = classof(value);
 8         console.log(str);
 9         switch(str){
10             case 'undefined': //undefined类型
11                 console.log(str);
12                 break;
13             case 'object': //null类型, 任意内置对象, 数组
14                 console.log(str);
15                 break;
16             case 'boolean': //true, false类型
17                 console.log(str);
18                 break;
19             case 'string': //string字符串类型
20                 console.log(str);
21                 break;
22             case 'function': //任意函数
23                 console.log(str);
24                 break;
25             case 'number'://任意的数值类型,包含NaN
26                 console.log(str);
27                 break;
28             case 'Date': //日期
29                 console.log(str);
30                 break;
31             case 'Array': //数组
32                 console.log(str);
33                 break;
34             case 'RegExp': //正则
35                 console.log(str);
36                 break;
37         }
38     }
39     //testType(function(){console.log(this)});//function
40     testType();

可以看到改进了一部分

2.使用instanceof检测对象类型

对于typeof()检测为Object类型的可以使用instanceof进一步检测具体类型.instanceof实际上检测的是对象的原型.

可以检测变量是不是某个对象的实例,返回bool值.

1 var value = new Date();
2 var isDate = value instanceof Date;
3 console.log(isDate);//true

3.使用constructor检测对象类型

constructor相当于检测构造函数,返回的是一个函数.

 1     function testconstructor(value) {
 2         var str = value.constructor;
 3         console.log(value.constructor);
 4         switch(str){
 5             case Number:
 6                 console.log(str);
 7                 break;
 8         }
 9     }
10     testconstructor(111); //function Number()

如果需要检测一个对象的确切类型,可以综合使用这三种方法:

 1 function type(o) {
 2         var t, c, n;//type class name
 3         //null类型
 4         if(o === null) return "null";
 5         //是数值中的特殊类型:NaN
 6         if(o !== o) return "nan";
 7         //使用typeof检测除去 "object" 类型为的其他类型.
 8         if((t = typeof o) !== "object") return t;
 9         //typeof检测为"object"类型,则进一步检测
10         //可以检测出大部分内置类型
11         if((c = classof(o)) !== "Object") return c;
12         //classof(o)返回为"Object"时,检测constructor
13         if(o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n;
14         //无法识别的其他类型,默认为"Object"
15         return "Object";
16     }
17     function classof(o) {
18         return Object.prototype.toString.call(o).slice(8,-1);
19     };
20 
21     //返回function的名称, 可能为""或者 null
22     Function.prototype.getName = function() {
23         if ("name" in this) return this.name;
24         return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
25     };
26     type(NaN);

还可以用jQuery来检测类型,常用的方法如下:

jQuery.isArray(obj);//检测对象是否是数组

1     jQuery.isFunction(obj);//测试对象是否为函数。
2     jQuery.isEmptyObject(obj);// jQuery 1.4 中,这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
3     jQuery.isPlainObject(obj);// 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
4     jQuery.isWindow(obj);// 测试对象是否是窗口(有可能是Frame)。
5     jQuery.type(obj);// 检测obj的数据类型。
6     jQuery.isNumeric(value);// 确定它的参数是否是一个数字,包含16进制数

---恢复内容结束---

 1     /**
 2      * 怎么检测一个变量的类型?
 3      * 在js中检测对象类型主要有三种:typeof, instanceof, constructor, 这几种都可以检测对象的类型.
 4      * 另外还可以适应jQuery来检测类型.
 5      * */
 6 
 7 
 8     /**
 9      * 1.使用typeof检测对象类型
10      * typeof作为最常用的检测类型方法,返回字符串类型;
11      * */
12     function testType(value) {
13         var str = typeof(value);
14         switch(str){
15             case 'undefined': //undefined类型
16                 console.log(str);
17                 break;
18             case 'object': //null类型, 任意内置对象, 数组
19                 console.log(str);
20                 break;
21             case 'boolean': //true, false类型
22                 console.log(str);
23                 break;
24             case 'string': //string字符串类型
25                 console.log(str);
26                 break;
27             case 'function': //任意函数
28                 console.log(str);
29                 break;
30             case 'number'://任意的数值类型,包含NaN
31                 console.log(str);
32                 break;
33         }
34     }
35     testType(111);
36     /**
37      * 可以看出typeof对于基本类型可以测试出类型,但是对于其他的,包括日期, 数组等等大多都返回object类型,而且null也返回的是object类型,也就是没有办法知道确切类型.
38      * */

另一种改进的检测方法, 是使用默认的toString(), 继承自Object,可以返回类型信息.

1     function classof(o){
2         if(o === null) return "Null";
3         if(o === undefined) return "Undefined";
4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
5     }

那为什么不直接用 a.toString() 而要用 Object.prototype.toString.call(a) ?

如果是数组或者函数,结果会是怎么样?

原因是:直接使用toString()有可能这个方法被重写了, 而不是默认的. 使用Object.prototype.toString().call(o).slice(8,-1);得到的结果更加可靠!

完整代码:

 1 function classof(o){
 2         if(o === null) return "Null";
 3         if(o === undefined) return "Undefined";
 4         return Object.prototype.toString.call(o).slice(8,-1);//获取对象类型
 5     }
 6     function testType(value) {
 7         var str = classof(value);
 8         console.log(str);
 9         switch(str){
10             case 'undefined': //undefined类型
11                 console.log(str);
12                 break;
13             case 'object': //null类型, 任意内置对象, 数组
14                 console.log(str);
15                 break;
16             case 'boolean': //true, false类型
17                 console.log(str);
18                 break;
19             case 'string': //string字符串类型
20                 console.log(str);
21                 break;
22             case 'function': //任意函数
23                 console.log(str);
24                 break;
25             case 'number'://任意的数值类型,包含NaN
26                 console.log(str);
27                 break;
28             case 'Date': //日期
29                 console.log(str);
30                 break;
31             case 'Array': //数组
32                 console.log(str);
33                 break;
34             case 'RegExp': //正则
35                 console.log(str);
36                 break;
37         }
38     }
39     //testType(function(){console.log(this)});//function
40     testType();

可以看到改进了一部分

2.使用instanceof检测对象类型

对于typeof()检测为Object类型的可以使用instanceof进一步检测具体类型.instanceof实际上检测的是对象的原型.

可以检测变量是不是某个对象的实例,返回bool值.

1 var value = new Date();
2 var isDate = value instanceof Date;
3 console.log(isDate);//true

3.使用constructor检测对象类型

constructor相当于检测构造函数,返回的是一个函数.

 1     function testconstructor(value) {
 2         var str = value.constructor;
 3         console.log(value.constructor);
 4         switch(str){
 5             case Number:
 6                 console.log(str);
 7                 break;
 8         }
 9     }
10     testconstructor(111); //function Number()

如果需要检测一个对象的确切类型,可以综合使用这三种方法:

 1 function type(o) {
 2         var t, c, n;//type class name
 3         //null类型
 4         if(o === null) return "null";
 5         //是数值中的特殊类型:NaN
 6         if(o !== o) return "nan";
 7         //使用typeof检测除去 "object" 类型为的其他类型.
 8         if((t = typeof o) !== "object") return t;
 9         //typeof检测为"object"类型,则进一步检测
10         //可以检测出大部分内置类型
11         if((c = classof(o)) !== "Object") return c;
12         //classof(o)返回为"Object"时,检测constructor
13         if(o.constructor && typeof o.constructor === "function" && (n = o.constructor.getName())) return n;
14         //无法识别的其他类型,默认为"Object"
15         return "Object";
16     }
17     function classof(o) {
18         return Object.prototype.toString.call(o).slice(8,-1);
19     };
20 
21     //返回function的名称, 可能为""或者 null
22     Function.prototype.getName = function() {
23         if ("name" in this) return this.name;
24         return this.name = this.toString().match(/function\s*([^(]*)\(/)[1];
25     };
26     type(NaN);

还可以用jQuery来检测类型,常用的方法如下:

jQuery.isArray(obj);//检测对象是否是数组

1     jQuery.isFunction(obj);//测试对象是否为函数。
2     jQuery.isEmptyObject(obj);// jQuery 1.4 中,这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
3     jQuery.isPlainObject(obj);// 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
4     jQuery.isWindow(obj);// 测试对象是否是窗口(有可能是Frame)。
5     jQuery.type(obj);// 检测obj的数据类型。
6     jQuery.isNumeric(value);// 确定它的参数是否是一个数字,包含16进制数

转载于:https://www.cnblogs.com/liubeimeng/p/5829440.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值