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属性 ...
随机推荐
- win10的power shell可以学习少部分linux命令_功能与cmd类似
更新帮助文件:
- 表单校验demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- poj 3013 SPFA
首先看题看的很懵.. 然后这题直接没想用Djstra做 TLE了.看discuss,Dijstra要用堆优化,也可以用SPFA做. 这里在网上找了这两种做法的区别,点多稠密图用Dij,以为它是操作点的 ...
- 从聚合数据请求菜谱大全接口数据,解析显示到ListView
- IT经典书籍——Head First系列…
Head First 系列书籍是由 O'Relly 出版社发行的一系列教育书籍,中文一般翻译为"深入浅出",它强调以特殊的方式排版,由大量的图片和有趣的内容组合构成,而达到非疲劳的 ...
- HTML canvas 笑脸
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 结对编程1---基于Flask的四则运算题目生成器
项目代码地址 / WEB应用地址 / 合作伙伴iFurySt博文链接 需求分析 本次程序是基于原有的控制台四则运算器的基础上,改成WEB的形式,同时还增加了一些新的功能.同时因为交互方式的改变,代码也 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.28
2017.04.28 天气晴朗 东风3级. 时间:上午 9:35 ---10:10分 地点:陆大二楼 会议内容:实验室报修系统项目冲刺Alpha版的的最后一天,大家对现在项目的进程进行了讨论,阐述了各 ...
- 201521123049 《JAVA程序设计》 第2周学习总结
*1. 本周学习总结 1.复习了一遍基本类型:整数类型(byte,short,int,long,char).浮点类型(float,double).boolean类型(true, false). 2.了 ...
- 201521123103 《java学习笔记》 第十二周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 二.书面作业 将Student对象(属性:int id, String name,int age,double ...