文章目录
1、直接返回数据类型
- 1、直接返回数据类型
- 2、构造函数返回布尔值Boolean
- 3、class返回布尔值Boolean-存在兼容问题-慎用
- 4、2和3的调用方式
function detectionType(type) { // 获取数据类型 type = Object.prototype.toString.call(type); // // 方式一 // 按空格切割字符串,并转成数组 // type = type.split(' ')[1]; // // 截取类型 // type = type.slice(0, type.length - 1); // // 转为小写 // 方式二 type = type.slice(8, type.length - 1); type = type.toLowerCase(); // 返回 return type; } console.log(detectionType('mj')); // string console.log(detectionType('')); // string console.log(detectionType(' ')); // string console.log(detectionType(1)); // number console.log(detectionType({ id: 1 })); // object console.log(detectionType({ id: 2, list: [7] })); // object console.log(detectionType([7])); // array console.log(detectionType([{ id: 1, list: [7] }])); // array console.log(detectionType(new Date())); // date console.log(detectionType(null)); // null console.log(detectionType()); // undefined console.log(detectionType(undefined)); // undefined console.log(detectionType(function () { })); // function console.log(detectionType(new RegExp())); // regexp console.log(detectionType(/runoob/i)); // regexp
☺☺☺☺☺☺☺
函数里第一行代码使用JavaScript最原始最可靠的方法获取数据类型,会得到这样一个字符串[object 数据类型],数据类型的第一个字母都是大写,所以第四行使用toLowerCase把字符串转为小写。第二行使用split方法把字符串按空格分割成数组,同时获取数组第二项的值。第三行通过slice方法截取数组第二项除]符号外的值,也就是数据类型。最后返回得到的数据类型。
2、构造函数返回布尔值Booleanfunction IsType(type) { this.type = type.toLowerCase(); this.checkType = function checkType(value) { value = Object.prototype.toString.call(value); value = value.slice(1, value.length - 1); value = value.split(' ')[1]; value = value.toLowerCase(); return value === this.type; } }
☺☺☺☺☺☺☺
函数里的第一行代码保存new时传入的值,并且使用toLowerCase转为小写取名为type再存储到this中。第二行代码是定义一个名为checkType的函数,并存储到this中。checkType函数里的第一行代码使用JavaScript最原始最可靠的方法获取数据类型,会得到这样的一个字符串[object 数据类型],数据类型的第一个字母都是大写,所以第四行使用toLowerCase转为小写。第二行代码使用slice截取得到这样的一个字符串object 数据类型。第三行使用split方法通过空格分割字符串成数组,并截取数组的第二个值,得到的值为数据类型。最后使用return返回匹配得到的布尔值Boolean。
3、class返回布尔值Boolean-存在兼容问题-慎用class IsType { constructor(type) { this.type = type.toLowerCase(); } checkType(value) { value = Object.prototype.toString.call(value); value = value.slice(1, value.length - 1); value = value.split(' ')[1]; value = value.toLowerCase(); return value === this.type; } }
☺☺☺☺☺☺☺
方案2与方案3是一样的逻辑,只是使用不同的实现方式而已,方案2使用了JavaScript最原始的构造函数方法,而方案3则使用ES6新增的class方式。
4、2和3的调用方式let s = new IsType('string'), num = new IsType('number'), o = new IsType('object'), a = new IsType('array'), d = new IsType('date'), nul = new IsType('null'), u = new IsType('undefined'), f = new IsType('function'), r = new IsType('regexp'); console.log(s.checkType('mj')); // true console.log(s.checkType('')); // true console.log(s.checkType(' ')); // true console.log(num.checkType(1)); // true console.log(o.checkType({ id: 1 })); // true console.log(o.checkType({ id: 2, list: [7] })); // true console.log(a.checkType([7])); // true console.log(a.checkType([{ id: 1, list: [7] }])); // true console.log(d.checkType(new Date())); // true console.log(nul.checkType(null)); // true console.log(u.checkType()); // true console.log(u.checkType(undefined)); // true console.log(f.checkType(function () { })); // true console.log(r.checkType(new RegExp())); // true console.log(r.checkType(/runoob/i)); // true