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


    一、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
  • 相关阅读:
    bower使用记录
    前端生成二维码
    删除顽固node_modules
    vue初体验:实现一个增删查改成绩单
    H5常用代码:适配方案5
    H5常用代码:适配方案4
    H5常用代码:适配方案3
    ARFA 教堂的第四次洗礼&斜率优化重学
    CSP考前总结&周二晚+周三晚模拟考总结&洛谷11月月赛 III Div.1总结
    T44253 绝美的挣扎 题解
  • 原文地址:https://www.cnblogs.com/le220/p/8427712.html
Copyright © 2020-2023  润新知