js中判断对象数据类型的方法
对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中判断对象数据类型的方法的更多相关文章
- js中判断对象具体类型
大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...
- js中判断对象是否存在
s中判断对象是否存在,写法有很多种: 第一种:if (!myObj) { var myObj = { }; }第二种:var global = this; if (!global.myObj) { ...
- js中判断对象类型的几种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- JS中判断对象是不是数组的方法
JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...
- js中Number对象与MATH方法整理总结
W3C的文档: Number 对象属性 属性 描述 constructor 返回对创建此对象的 Number 函数的引用. MAX_VALUE 可表示的最大的数. MIN_VALUE 可表示的最小的数 ...
- js中数组对象去重的方法
var arr = [{ key: '01', value: '乐乐' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' },{ key: ...
- js中Window 对象及其的方法
window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...
- JS中string对象的一些方法
原文地址(包含所有的string对象的方法): http://www.dreamdu.com/javascript/object_string/ string.slice(startPos,endP ...
- JS中String对象常用的方法
1. stringObject.charAt(index) 参数:index 必需,即字符在字符串中的下标. 返回值: 返回在指定位置的字符.返回的字符是长度为 1的字符串.(length属性 ...
随机推荐
- 【RN6752】模拟高清AHD芯片或成为车机新标配
由于目前车机方案倒车后视目前大多为480P标配,画面噪点多有锯齿成像效果差,成为行业的难言之痛.2012年底,浙江大华宣布发布一项具备完全自主知识产权的安防行业高清视频传输规范--HDCVI--Hig ...
- SQL优化工具
SQL优化工具 什么是索引? 打个比方,我们在使用MySQL用作查询的时候就好比查字典,索引就好比字典的偏旁部首页.如果没有索引我们查询一个文字就需要一页页的翻,显然这种方式效率很低.如果我们对某一字 ...
- BZOJ 3379: [Usaco2004 Open]Turning in Homework 交作业
Description 贝茜有C(1≤C≤1000)门科目的作业要上交,之后她要去坐巴士和奶牛同学回家. 每门科目的老师所在的教室排列在一条长为H(1≤H≤1000)的走廊上,他们只在课后接收 ...
- Mysql分区表使用的一些限制和需要注意的地方
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt347 mysql分区策略都基于两个非常重要的假设:查询都能够过滤(prunn ...
- angularJS+Ionic移动端图片上传的解决办法
前端开发中经常会碰到图片上传的问题,网上的解决办法很多,可是有些图片上传的插件会有一些附属的插件,因此因为一个图片上传的问题可能额需要引入其他插件到项目中,久而久之项目会不伦不类,有时候插件之间也会有 ...
- c# HttpWebRequest 模拟HTTP post 传递JSON参数
//HTTP post JSON 参数 private string HttpPost(string Url, Object ticket) { ...
- KVM虚拟化主机安装
KVM虚拟化主机安装 最小化安装CentOS6.X或者CentOS7.X,RHEL6.X以上系列建议建议选择安装最小虚拟化主机 如果要安装桌面可以先选择最小化虚拟主机,再选择Gnome桌面包 安装过程 ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.28
团队作业4--第一次项目冲刺(Alpha版本) Day seven: 会议照片 每日站立会议: 项目进展 今天是项目的Alpha敏捷冲刺的第七天,先大概整理下昨天已完成的任务以及今天计划完成的任务.今 ...
- 201521123092《java程序设计》第八周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4 ...
- 201521123006 《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boo ...