js 判断数据类型

常用判断方法:

1. typeof

typeof 共返回6种数据格式,只能判断区分基本类型: number、string、boolean、undefined、object 以及 function

typeof 0;             //number
typeof true;          //boolean
typeof "string";      //string

typeof undefined;         //undefined
typeof function(){};      //function

typeof null;   //object
typeof {};     //object
typeof [];     //object    

对象和数组 无法区分。

2. instanceof

instanceof 运算符用来检测 右侧 constructor.prototype 是否存在于 左侧参数 object 的原型链上。即:

A instanceof B   // 检测构造函数B的原型是否有出现在对象A的原型链上。

因此,instanceof 运算符只能用于对象,不能用于原始类型的值

let a = true
let b = new Boolean(true)
a instanceof Boolean          // false
b instanceof Boolean          // true

检测引用类型时,对于数组的校验也是不尽人意

let c={};
c instanceof Object   //true
c instanceof Array    //false
let d=[];
d instanceof Object   //true
d instanceof Array    //true

3. constructor

constructor可以区分Array和Object,也能支持各种类型的判断,如下

(1).constructor === Number                     // true
(true).constructor === Boolean                 // true
('string').constructor === String              // true
([]).constructor === Array                     // true
(function() {}).constructor === Function       // true
({}).constructor === Object                    // true

但是,存在两大缺陷

(1)null 和 undefined 是无效的对象,不存在 constructor ,无法以此来作判断。

(2)constructor 是不稳定的,这个主要体现在把类的原型进行重写,在重写的过程中很有可能出现把之前的 constructor 给覆盖了,这样检测出来的结果就是不准确的

function Fn(){};
 
Fn.prototype=new Array();
 
var f=new Fn();
 
console.log(f.constructor===Fn);    // false
console.log(f.constructor===Array); // true

4. Object.prototype.toString.call()

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

Object.prototype.toString.call(1)
//"[object Number]"

Object.prototype.toString.call('string')
//"[object String]"

Object.prototype.toString.call(true)
//"[object Boolean]"

Object.prototype.toString.call([])
//"[object Array]"

Object.prototype.toString.call({})
//"[object Object]"

Object.prototype.toString.call(function(){})
//"[object Function]"

Object.prototype.toString.call(undefined)
//"[object Undefined]"

Object.prototype.toString.call(null)
//"[object Null]"

prototype.js 中用于获取对象的类型名

/**
 * Returns internal [[Class]] property of an object
 *
 * Ecma-262, 15.2.4.2
 * Object.prototype.toString( )
 *
 * When the toString method is called, the following steps are taken: 
 * 1. Get the [[Class]] property of this object. 
 * 2. Compute a string value by concatenating the three strings "[object ", Result (1), and "]". 
 * 3. Return Result (2).
 *
 * __getClass(5); // => "Number"
 * __getClass({}); // => "Object"
 * __getClass(/foo/); // => "RegExp"
 * __getClass(''); // => "String"
 * __getClass(true); // => "Boolean"
 * __getClass([]); // => "Array"
 * __getClass(undefined); // => "Window"
 * __getClass(Element); // => "Constructor"
 *
 */
function __getClass(object){
  return Object.prototype.toString.call(object).match(/^[objects(.*)]$/)[1];
};

借此,可扩展为 js 工具:

var is ={
  types : ["Array", "Boolean", "Date", "Number", "Object", "RegExp", "String", "Window", "HTMLDocument"]
};
for(var i = 0, c; c = is.types[i]; i ++){
  is[c] = (function(type){
    return function(obj){
      return Object.prototype.toString.call(obj) == "[object " + type + "]";
    }
  })(c);
}
console.log(is.Array([]));       // true
console.log(is.Date(new Date));    // true
console.log(is.RegExp(/reg/ig));   // true
原文地址:https://www.cnblogs.com/_error/p/9856796.html