面试题之JavaScript 请编写实现一个对js类型检测函数(支持检测基本类型,对象,函数,正则,时间等)

小编才疏学浅,若文章、答案有错误之处,欢迎邮件通知小编及时修改,同时也可以面试题投稿。最后祝大家共同进步!

javascript 中有几种类型的值

1.基本数据类型 : 包括 Undefined、Null、Boolean、Number、String、Symbol (ES6 新增,表示独一无二的值)

特点:
a. 值不可变
b. 存放在栈中
c.双等和全等的区分

2.引用数据类型: 包括 Object、Array、Function

特点:
a.值可变
b.同时保存再栈内存和堆内存
c.比较是引用的比较

javascript 数据类型的检测

  1. typeof : 返回一个表示数据类型的字符串(number boolean string symbol object undefined function), 缺点: 不能判断 null array 时间对象 正则对象
typeof Symbol(); // symbol 有效
typeof ''; // string 有效
typeof 1; // number 有效
typeof true; //boolean 有效
typeof undefined; //undefined 有效
typeof new Function(); // function 有效
typeof null; //object 无效
typeof [] ; //object 无效
typeof new Date(); //object 无效
typeof new RegExp(); //object 无效

null 和 array 都会返回 object

  1. indestanceof : 判断 A 是否是 B 的实例 (A instanceof B),返回布尔值,便是一个对象在其原型链中是否存在一个构造函数的prototype属性。
    缺点:
    a.字面量创建和实例方式创建有区别,只有实例创建的结果才是标准的。
console.log(1 instanceof Number)//false
console.log(new Number(1) instanceof Number)//true

b.只要再当前的实例原型链上,检测结果都为true

var arr = [1, 2, 3];
console.log(arr instanceof Array) // true
console.log(arr instanceof Object);  // true
function fn(){}
console.log(fn instanceof Function)// true
console.log(fn instanceof Object)// true

c.不能检测 null 和 undefined: 对于特殊的数据类型 null 和 undefined,他们的所属类是 Null 和 Undefined,但是浏览器把这两个类保护起来了,不允许我们在外面访问使用。

  1. 使用instanceof检测 数组、对象、时间对象、正则对象
[] instanceof Array; //true
{} instanceof Object;//true
new Date() instanceof Date;//true
new RegExp() instanceof RegExp//true
  1. constructor: constructor 检测 Object 与 instanceof 不一样,还可以处理基本数据类型的检测。
var aa=[1,2];
console.log(aa.constructor===Array);//true
console.log(aa.constructor===RegExp);//false
console.log((1).constructor===Number);//true
var reg=/^$/;
console.log(reg.constructor===RegExp);//true
console.log(reg.constructor===Object);//false

缺点:
a. null 和 undefined 是无效的对象,因此是不会有 constructor 存在的
b.函数可以把类的原型进行重写,所以检测不稳定

  1. Object.prototype.toString.call() : Object.prototype.toString.call() 最准确最常用的方式。首先获取 Object 原型上的 toString 方法,让方法执行,让 toString 方法中的 this 指向第一个参数的值。


Object对象和它的原型链上各自有一个toString()方法,第一个返回的是一个函数,第二个返回的是值类型。

一般情况下,js中对象的toString(),返回字符串,内容与函数声明语法有关,例如[1,2,3].toString()//"1,2,3"

大多数都返回函数的完整源码,Array.toString()//"function Array() { [native code] }"

内置函数往往返回一个类似"[native code]"的函数体,需要配合call方法,比如Object.prototype.toString.call([1,2,3])//"[object Array]"

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window是全局对象global的引用

文章标题答案

    function getType(target) {
      var ret = typeof (target);
      var template = {
        "[object Array]": "array",
        "[object Date]": "date",
        "[object RegExp]": "regexp",
        "[object Object]": "object"
      }
      if (target === null) {
        return 'null';
      } else if (ret == "object") {
        var str = Object.prototype.toString.call(target);
        return template[str];
      } else {
        return ret;
      }
    }
    console.log(getType( Symbol())); // symbol 有效
    console.log(getType( '')); // string 有效
    console.log(getType( 1)); // number 有效
    console.log(getType( true)); //boolean 有效
    console.log(getType( undefined)); //undefined 有效
    console.log(getType( new Function())); // function 有效
    console.log(getType( null)); //null 有效
    console.log(getType( [] )); //array 有效
    console.log(getType( new Date())); //data 有效
    console.log(getType( new RegExp())); //regexp 有效

原文:
js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型

原文地址:https://www.cnblogs.com/jiaoshou/p/13720093.html