首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第173天:面向对象——数据类型检测的四种方法

第173天:面向对象——数据类型检测的四种方法

作者头像
半指温柔乐
发布2018-09-11 09:59:51
2900
发布2018-09-11 09:59:51
举报
文章被收录于专栏:前端知识分享前端知识分享

一、typeof

1 console.log(typeof 12); // number
2 var str = 'iceman';
3 console.log(typeof str); // string

使用typeof检测数据类型,首先返回的都是一个字符串,其次字符串中包含了对应的数据类型,例如:"number"、"string"、"boolean"、"undefined"、"function"、"object"

面试题:

1 console.log(typeof typeof typeof function () {}); // string

typeof的局限性:不能具体的细分是数组还是正则,还是对象中其他的值,因为使用typeof检测数据类型,对于对象数据类型中的所有的值,最后返回的结果都是"object"。

应用一:添加默认值

 1 function fn(num1, num2) {
 2     // 方式一
 3     // if (typeof num2 === 'undefined') {
 4     //   num2 = 0;
 5     // }
 6 
 7     // 方式二:不适用fn(10,false)这种情况
 8     num2 = num2 || 0;
 9 }
10 fn(10);

应用二:回调函数调用

1 function fn(callback) {
2     //typeof callback === 'function' ? callback() : null;
3     callback && callback();
4 }
5 fn(function () {
6     
7 });

二、instanceof

1 var ary = [];
2 console.log(ary instanceof Array); // true
3 console.log(ary instanceof Object); // true
4 
5 function fn() {}
6 console.log(fn instanceof Function); // true
7 console.log(fn instanceof Object); // true

只要在当前实例的原型链上,用instanceof检测出来的结果都是true,所以在类的原型继承中,最后检测出来的结果未必是正确的,例如:

1 function Fn() {
2 }
3 Fn.prototype = new Array; // 原型继承:让子类的原型等于父类的一个实例
4 var f = new Fn;
5 console.log(f instanceof Array); // true

三、constructor

constructor即构造函数,作用和instanceof非常的相似。

1 var obj = [];
2 console.log(obj.constructor === Array); // true
3 console.log(obj.constructor === RegExp); // false

constructor可以处理基本数据类型的检测

1 var num = 1;
2 console.log(num.constructor === Number); // true

constructor检测Object和instanceof不一样,一般情况下是检测不了的

1 var reg = /^$/;
2 console.log(reg.constructor === RegExp); // true
3 console.log(reg.constructor === Object); // false

constructor的局限性:我们可以把类的原型进行重写,在重写的过程中,很有可能把之前的constructor给覆盖了,这样检测出来的结果就是不准确的。

1 function Fn() {
2 }
3 Fn.prototype = new Array;
4 var f = new Fn;
5 console.log(f.constructor); // Array

对于特殊的数据类型nullundefined,它们的所属类型是NullUndefined,但是浏览器把这两个类保护起来了,不允许在外面访问使用。

四、Object.prototype.toString.call()

toString的理解:

  • 咋一看应该是转换为字符串,但是某些toString方法不仅仅是转换为字符串。对于Number、String、Boolean、Array、RegExp、Date、Function原型上的toString方法都是把当前的数据类转换为字符串的类型(它们的作用仅仅是用来转换为字符串的)
  • Object.prototype.toString并不是用来转换为字符串的。
1 ({name:"iceman"}).toString() // --> "[object Object]"
2  Math.toString() // --> "[object Math]"

Number.prototype.toString是转换字符串的

1 console.log((1).toString()); // --> Number.prototype.toString,--> "1"  转换为字符串
2 console.log((1).__proto__.__proto__.toString()); // Object.prototype.toString --> "[object Object]"
3 console.log((128).toString(2/8/10)); // 把数字转换为二进制/八进制/十进制

Object.prototype.toStrong.call()是检测数据类型最准确最常用的方式,其原理为:

  • 先获取Object原型上的toString方法,让方法执行,并且改变方法中的this关键字的指向;
  • Object.prototype.toString 它的作用是返回当前方法的执行主体(方法中this)所属类的详细信息;
1 var obj = {name:'iceman'};
2 // toString中的this是obj,返回的是obj所属类的信息 --> "[object Object]"
3 // 第一个object代表当前实例是对象数据类型的(这个是固定死的)
4 // 第二个Object,代表的是obj所属的类是Object
5 console.log(obj.toString());
6 
7 // toString中的this是Math,那么返回的是Math所属类的信息 --> "[object Math]"
8 console.log(Math.toString());

检测其他类型:

 1 var ary = [];
 2 console.log(Object.prototype.toString.call(ary)); // --> "[object Array]"
 3 console.log(Object.prototype.toString.call(/^$/)); // --> "[object RegExp]"
 4 console.log(({}).toString.call(1)); // --> "[object Number]"
 5 
 6 console.log(({}).toString.call('珠峰')); // --> "[object String]"
 7 console.log(({}).toString.call(true)); // --> [object Boolean]
 8 console.log(({}).toString.call(undefined)); // -->[object Undefined]
 9 console.log(({}).toString.call(null)); // -->[object Null]
10 console.log(({}).toString.call(function () {})); // -->[object Function]

实际使用:

1 var ary = [];
2 console.log(Object.prototype.toString.call(ary) === '[object Array]'); // true
3 
4 var reg = /^\[object Array\]$/;
5 console.log(reg.test(Object.prototype.toString.call(ary))); // true

检测原型继承的情况:

1 function Fn() {
2 }
3 Fn.prototype = new Array;
4 var f = new Fn;
5 console.log(f instanceof Array); // true
6 console.log(Object.prototype.toString.call(f) === '[object Array]'); // false
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、typeof
  • 二、instanceof
  • 三、constructor
  • 四、Object.prototype.toString.call()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档