美文网首页JavaScript技术
JS 判断对象类型

JS 判断对象类型

作者: Cherry丶小丸子 | 来源:发表于2021-09-26 14:19 被阅读0次
1. typeof
`typeof只能判断区分基本类型,number、string、boolean、undefined 和 object、function`

typeof 0; // number;
typeof true; // boolean;
typeof undefined;  // undefined;
typeof "hello world"; // string;
typeof function(){}; // function;

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

let s = Symbol();
typeof s; // "symbol"
s instanceof Symbol; // false

`从上例我们可以看出, typeof  判断对象和数组都返回 object,因此它无法区分对象和数组`
2.instanceof
let a = {};
a instanceof Object; // true
a instanceof Array; // false
let b = [];
b instanceof Array; // true
b instanceof Object; // true

`因为数组属于 object 中的一种,所以数组 instanceof object,也是true`

let c = 'abc';
c instanceof String; // false
let d = new String();
d instanceof String; // true

`instanceof 不能区分基本类型 string 和 boolean,除非是字符串对象和布尔对象`
3.constructor
let o = {};
o.constructor == Object; // true
let arr = [];
arr.constructor == Array; // true
arr.constructor == Object; // false

`可以看出 constructor 可以区分 Array 和 Object`

let n = true;
n.constructor == Boolean; // true
let num = 1;
num.constructor == Number; // true
let str = 'hello world';
str.constructor == String; // true
let num = new Number();
num.constructor == Number; // true

`不过要注意,constructor属性是可以被修改的,会导致检测出的结果不正确`

function Person(){}
function Student(){}
Student.prototype = new Person();
let John = new Student();
console.log(John.constructor == Student); // false
console.log(John.constructor == Person); // true

`除了 undefined 和 null,其他类型的变量均能使用 constructor 判断出类型`
4.Object.prototype.toString.call() ---------最好用
Object.prototype.toString.call(123); // "[object Number]"
Object.prototype.toString.call('str'); // "[object String]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"

`封装一个判断数组和对象的方法`
function typeObj(obj){
    let type = Object.prototype.toString.call(obj);
    if(type == '[object Object]'){
        return 'Object';
    }else if(type == '[object Array]'){
        return 'Array';
    }else{
        return 'obj is not Object or Array';
    }
}

// 全类型判断
function typeObj(obj){
    let type = Object.prototype.toString.call(obj);
    if(type == '[object Number]'){
        return 'Number';
    }else if(type == '[object String]'){
        return 'String';
    }else if(type == '[object Boolean]'){
        return 'Boolean';
    }else if(type == '[object Object]'){
        return 'Object';
    }else if(type == '[object Array]'){
        return 'Array';
    }else if(type == '[object Null]'){
        return 'Null';
    }else if(type == '[object Undefined]'){
        return 'Undefined';
    }
}
5.jQuery中的 $.type接口
$.type(obj) ;
$.isArray(obj);
$.isFunction(obj);
$.isPlainObject(obj);


$.type(null); // null
$.type([]); // array
$.isArray([]); // true
$.isFunction(function(){}); // true
$.isPlainObject({}); // true
$.isPlainObject([]); // false
$.isPlainObject(null); // false

相关文章

网友评论

    本文标题:JS 判断对象类型

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