美文网首页
检测数据类型的四种方式 ------ 2020-06-20

检测数据类型的四种方式 ------ 2020-06-20

作者: 自己写了自己看 | 来源:发表于2020-08-15 17:30 被阅读0次

1、 typeof

/**
  * typeof 用来检测数据类型的运算符
  * 
  * @用法: typeof [value]
  * 
  * @return 
  *   (1)首先返回值是一个字符串
  *   (2)字符串中包含对应的数据类型,例如:
  *        "string"/"number"/"object"/"undefined"/"function"/"boolean"/"symbol"
  * 
  * @局限性
  *   (1)typeof null => 'object'
  *    对于引用数据类型值,不能具体区分
  *    typeof {}     => 'object'
  *    typeof []     => 'object'
  *    typeof /^$/   => 'object'
  * 
  * @优势
  *  使用方便,所以在真实项目中,我们也会大量应用它来检测,尤其是在检测
  *  基本数据类型值(除了null)的时候,它还是很方便的
 */

2、instanceof

/**
 * instanceof: 本意是用来检测实例是否隶属于某个类
 * 的运算符,我们基于这样的方式,也可以用来做某些数
 * 据类型的检测,例如:数组、正则等
 * 
 * @局限性
 *  (1)不能处理基本数据类型的值;
 *  (2)只要在当前实例的原型链(__proto__)中出现
 *       过的类,检测结果都为true,用户可能会手动修
 *       改原型链的指向,造成结果检测不准确;
 *   function func () {
 *       // arguments: 类数组
 *       console.log(arguments instanceof Array); // => false
 *       arguments.__proto__ = Array.prototype 
 *       console.log(arguments instanceof Array); // => true
 *   }
*/

let arr = [];
let obj = {};
let reg = /^s/;
console.log( arr instanceof Array); // => true
console.log( arr instanceof Object); // => true

console.log( obj instanceof Object); // => true

console.log(reg instanceof RegExp); // => true
console.log(reg instanceof Object); // => true

3、constructor

/**
 * constructor: 构造函数
 * 
 * @原理:在类的原型上一般都会带有constructor属性,存储着当前类本身,
 * 我们也是利用这一点,获取某个实例的constructor属性值,验证是否为了
 * 所属类,从而进行数据类型检测
 * 
 * @属性值: constructor属性很容易被修改
*/

let n = 12,
    arr = [];
console.log(n.constructor === Number); // => true
console.log(arr.constructor === Array); // => true
console.log(arr.constructor === Object); // => false

arr.constructor = 111;
console.log(arr.constructor === Array); // => false

4、Object.prototype.toString().call([value])

/**
 * Object.prototype.toString.call([value]): 调用
 * Object原型上的toString()方法,让方法执行,toString()
 * 方法中的this是要检测的数据类型,从而获取到数据类型所属
 * 类的详细信息
 * 
 * @详细信息模板
 *    "[object 所属类]",例如:"[object Array]"
 * 
 * @ 注意点:
 * 在所有的数据类型中,他们的原型上都有toString()方法,除了
 * Object.prototype.toString()不是把数据值转换为字符串,
 * 其余都是
 * 
 * @ Object实例调取 toString()方法:
 *    obj.toString();
 *   (1)首先基于原型链的查找机制,找到Object.prototype.toString();
 *   (2)把找到的方法执行,方法中的this => obj;
 *   (3)方法内部把this(obj)所属类的详细信息输出;
 *  方法执行,方法中的this是谁,就是检测谁的所属类的信息;
*/
function func () {
    return n + m;
}
let obj1 = {},
    obj2 = {
        name:'test'
    }

console.log([1,2].toString()); // => "1,2"
console.log(/^\d+$/.toString()); // => "/^\d+$/"
console.log(func.toString()); // => "func () {return n + m;}"
console.log(obj1.toString()); // => "[object Object]"
console.log(obj2.toString()); // => "[object Object]"


console.log(Object.prototype.toString.call(func)); // => [object Function]

5、封装一个万能的检测数据类型的方法:

 let _obj = {
        isNumberic: 'Number',
        isBoolean: 'Boolean',
        isString: 'String',
        isNull: 'Null',
        isUndefined: 'Undefined',
        isSymbol: 'Symbol',
        isPlainObject: 'Object',
        isArray: 'Array',
        isRegExp: 'RegExp',
        isDate: 'Date',
        isFunction: 'Function',
        isWindow: 'Window'
    },
        _toString = _obj.toString,
        _type = {};

    for (const key in _obj) {
        if (!_obj.hasOwnProperty(key)) break;
        let reg = new RegExp("\\[object " + _obj[key] + "\\]");
        _type[key] = function anonymous(val) {
            return reg.test(_toString.call(val));
        }
    }

console.log(_type.isFunction(1)); // => false;

相关文章

网友评论

      本文标题:检测数据类型的四种方式 ------ 2020-06-20

      本文链接:https://www.haomeiwen.com/subject/cykwxktx.html