对js中不同数据的布尔值类型总结:false:空字符串;null;undefined;0;NaN。
true:除了上面的false的情况其他都为true;

如下:

var o = {
'name':'lee'
};
var a = ['reg','blue'];
function checkBoolean(a){
if(a){
return true;
}else{
return false;
}
}
console.log(checkBoolean('')); //false
console.log(checkBoolean(0)); //false
console.log(checkBoolean(null)); //false
console.log(checkBoolean(undefined)); //false
console.log(checkBoolean(NaN)); //false
console.log(checkBoolean(a));//true
console.log(checkBoolean(c));//true

javascript中有六种数据类型:string;boolean;Array;Object;null;undefined。如何检测这些数据类型呢,总结方法如下:

方法一:采用typeof

       var fn = function(n){
console.log(n);
}
var str = 'string';
var arr = [1,2,3];
var obj = {
a:123,
b:456
};
var num = 1;
var b = true;
var n = null; var u = undefined;
//方法一使用typeof方法。
console.log(typeof str);//string
console.log(typeof arr);//object
console.log(typeof obj);//object
console.log(typeof num);//number
console.log(typeof b);//boolean
console.log(typeof n);//null是一个空的对象
console.log(typeof u);//undefined
console.log(typeof fn);//function

通过上面的检测我们发现typeof检测的Array和Object的返回类型都是Object,因此用typeof是无法检测出来数组和对象的,采用方法二和方法三则可以检测出来。

方法二:instanceof

 var o = {
'name':'lee'
};
var a = ['reg','blue'];
console.log(o instanceof Object);// true
console.log(a instanceof Array);// true
console.log(o instanceof Array);// false

注意:instaceof只可以用来判断数组和对象,不能判断string和boolean类型,要判断string和boolean类型需要采用方法四。
 由于数组也属于对象因此我们使用instanceof判断一个数组是否为对象的时候结果也会是true。如:

console.log(a instanceof Object);//true。

下面封装一个方法进行改进:

var o = {
'name':'lee'
};
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Array){
return 'Array'
}else if( o instanceof Object ){
return 'Object';
}else{
return 'param is no object type';
}
};
console.log(getDataType(o));//Object。
console.log(getDataType(a));//Array。

方法三:使用constructor方法

var o = {
'name':'lee'
};
var a = ['reg','blue'];
console.log(o.constructor == Object);//true
console.log(a.constructor == Array);//true

方法四:利用tostring()方法,这个方法是最佳的方案。

var o = {
'name':'lee'
};
var a = ['reg','blue'];
function c(name,age){
this.name = name;
this.age = age;
}
var c = new c('kingw','27');
console.log(Object.prototype.toString.call(a));//[object Array]
console.log(Object.prototype.toString.call(o));//[Object Object]
console.log(Object.prototype.toString.call(c));//[Object Object] //封装一个方法判断数组和对象
function isType(obj){
var type = Object.prototype.toString.call(obj);
if(type == '[object Array]'){
return 'Array';
}else if(type == '[object Object]'){
return "Object"
}else{
return 'param is no object type';
}
}
console.log(isType(o));//Object
console.log(isType(a));//Array //下面是更简洁的封装,来自vue源码 var _toString = Object.prototype.toString;

function toRawType (value) {return _toString.call(value).slice(8, -1)}
 

方法五:利用jquery的$.isPlainObject();$.isArray(obj);$.isFunction(obj)进行判断。

js中判断对象数据类型的方法的更多相关文章

  1. js中判断对象具体类型

    大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...

  2. js中判断对象是否存在

    s中判断对象是否存在,写法有很多种: 第一种:if (!myObj) { var myObj = { }; }第二种:var global = this;  if (!global.myObj) {  ...

  3. js中判断对象类型的几种方法

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...

  4. JS中判断对象是不是数组的方法

    JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...

  5. js中Number对象与MATH方法整理总结

    W3C的文档: Number 对象属性 属性 描述 constructor 返回对创建此对象的 Number 函数的引用. MAX_VALUE 可表示的最大的数. MIN_VALUE 可表示的最小的数 ...

  6. js中数组对象去重的方法

    var arr = [{ key: '01', value: '乐乐' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' },{ key: ...

  7. js中Window 对象及其的方法

    window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...

  8. JS中string对象的一些方法

    原文地址(包含所有的string对象的方法):  http://www.dreamdu.com/javascript/object_string/ string.slice(startPos,endP ...

  9. JS中String对象常用的方法

    1.  stringObject.charAt(index) 参数:index 必需,即字符在字符串中的下标.  返回值:   返回在指定位置的字符.返回的字符是长度为 1的字符串.(length属性 ...

随机推荐

  1. Python学习笔记开篇

    已经快30岁了,中专学历,不会什么技术,因为好吃懒做最喜欢的就是吃肉睡觉. 每次想学习技术如PhotoShop,绘声绘影,PHP,易语言,按键精灵都只是3分钟热血. 今天我想在业余时间学习Python ...

  2. vue.js基础知识篇(4):过滤器、class与style的绑定2

    代码下载:网盘 欢迎私信 第一章:过滤器 过滤器是对数据进行处理并返回结果的函数. 1.语法 语法是使用管道符"|"进行连接.过滤器可以接收参数,跟在过滤器后面,带引号的参数被当做 ...

  3. B/S 架构中,网络模型的分解与协议解析

    前言 如果是C/S专业毕业的或者是学过计算机网络课程的童鞋们,相信大家都知道网络模型的划分,本文首先来聊一聊目前对于B/S结构中,网络模型分解的两种方式. 没错,相信大家看到这个图片的时候就已经明白了 ...

  4. spring mvc 处理流程整理

    1.  首先用户发送请求-->DispatcherServlet,前端控制器收到请求后自己不进行处理,而是委托给其他的解析器进行处理,作为统一访问点,进行全局的流程控制: 2.  Dispatc ...

  5. 2017年8月28日 HTML/CSS 语法(待填坑)

    今天这种节日真的是 ----------------------------------------------------------- HTML  

  6. Jmeter之Bean shell学习(一)

    一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...

  7. element ui datePicker 设置当前日期之前的日期不可选

    pickerOptions0: { disabledDate(time) { return time.getTime() < Date.now() - 8.64e7 } },

  8. spring整合mybatis错误:HTTP Status 404 - xxx-xxx....

    运行环境:jdk1.7.0_17 + tomcat 7 + spring 3.2.0 +mybatis 3.2.7+ eclipse,访问路径:http://localhost:8085/Spring ...

  9. 关于hashmap的理解

    首先分析第一个比较重要的方法 put 方法,源码如下 public V put(K key, V value) { if (key == null) return putForNullKey(valu ...

  10. (3)ES6解构赋值-对象篇

    对象的解构赋值(可以不按顺序,但是key必须一样否则为undefined) //demo1 var {name,age} = { name: "Jewave", age:26 }; ...