详解javascript类型判断

引言

javascript数据有六大基本类型:Number,String,Boolean,Null,Undefined,Symbol,一种引用类型Objectjavascript

typeof检测基本类型

typeof能够用来检测除null以外的基本类型,还能够检测函数
图片描述
图片描述java

instanceof检测对象类型

图片描述
注意:instanceof 后面必定要是对象类型,而且大小写不能错,该方法适合一些条件选择或分支数组

通用方法Object.prototype.toString.call(arg)

大小写不能写错,比较麻烦,但胜在通用。
图片描述函数

扩展阅读

zepto封装的判断是否数组方法:spa

isArray = Array.isArray ||function(object){ return object instanceof Array }

underscore判断给定变量是不是对象prototype

// Is a given variable an object?
_.isObject = function(obj) {
    var type = typeof obj;
    return type === 'function' || type === 'object' && !!obj;
};
相关文章
相关标签/搜索