对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. [读书笔记] 一、Spring boot项目搭建与配置文件

    读书笔记:[JavaEE开发的颠覆者 Spring Boot实战] 作者:汪云飞 从今天开始坚持读书,并记录下此读书笔记. 一,初接触 Spring boot 项目Hello world搭建 1.po ...

  2. Java后台开发必备软件(windows环境下)

    一.必备软件 1.Jdk,推荐下载最新版2.Ide,推荐 IntelliJ IDEA3.服务器,如 tomcat / jetty4.数据库终端界面,推荐 Navicat Premium(自行破解),5 ...

  3. SpringMVC入门第二天

    SpringMVC第二天 1.   回顾 1.Springmvc介绍? Springmvc是Spring公司 2.Springmvc入门程序 第一步:Web工程 第二步:导Jar包 第三步:web.x ...

  4. Asp.net mvc3的“从客户端中检测到有潜在危险的 Request.Form 值”问题解决

    Asp.net mvc3的“从客户端(content_v=\",<p>\n\t<imgalt=\"\" src...\")中检测到有潜在危险的 ...

  5. 大道至简第一章观后感——java伪代码

    一节: public class Yugongyishan_ { //定义一个名为Yugongyishan_的类 Public static void main(string args[])   // ...

  6. 一种解决url的get请求参数传值乱码问题的方式

    做项目的时候发现url get请求传中文字符出现乱码问题,百度了一下,最后用一种比较容易理解的方式解决了.分享给大家! 经过百度,网友提到:url get方式提交的参数编码,只支持iso8859-1编 ...

  7. Python Keras module 'keras.backend' has no attribute 'image_data_format'

    问题: 当使用Keras运行示例程序mnist_cnn时,出现如下错误: 'keras.backend' has no attribute 'image_data_format' 程序路径https: ...

  8. windows下使用密钥登录Linux及xshell代理转发

    1.密钥登录原理 一般我们使用xshell访问远程主机(Linux主机)时,都是先请管理员给我们开一个账户,即为我们设置一个一个用户名和对应的密码,然后我们就可以使用下面的方式登录到远程主机了: 在这 ...

  9. 个人作业3-个人总结(Alpha阶段)

    一.个人总结 1.团队状况:这是我们第一次团队开发,小组成员的编程水平都相对一般,要在一周内完成一个APP是一个很大的考验.再加上冲刺的那周团队大半成员都在为一个比赛培训,时间就更少了,曾经有很多次我 ...

  10. 201521123101 《Java程序设计》第2周学习总结

    1. 本周学习总结 使用码云保存管理自己的代码: 学习String和Array: 继续对JAVA的探索,希望以后能在编程上更顺畅一些 2. 书面作业 1.使用Eclipse关联jdk源代码(截图),并 ...