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属性 ...
随机推荐
- jquery的2.0.3版本源码系列(3):96行-283行,给JQ对象,添加一些方法和属性
jquery是面向对象的程序,面向对象就离不开方法和属性. 方法的简化 jQuery.fn=jQuery.prototype={ jquery: 版本 constructor: 修正指向问题 init ...
- python在windows和linux环境的进程对比
1.fork进程:(1)在windows系统中不可以用fork来创建进程,linux可以,但是创建大量进程使用很不方便. 2.Process进程: import multiprocessing as ...
- html+css手记
----------------------html定义和基本结构---------------------- HTML是 HyperText Mark-up Language 的首字母简写,意思是超 ...
- [js高手之路]深入浅出webpack教程系列4-插件使用之html-webpack-plugin配置(上)
还记得我们上文中的index.html文件吗? 那里面的script标签还是写死的index.bundle.js文件,那么怎么把他们变成动态的index.html文件,这个动态生成的index.htm ...
- border-radius值的解析
border-radius: none | length{1,4} / length{1,4} 其中每一个值可以为 数值或百分比的形式. length/length 第一个lenght表示水平方向的半 ...
- 第1阶段——uboot分析之通过nand命令读内核(8)
本节主要学习: 详细分析UBOOT中"bootcmd=nand read.jffs2 0x30007FC0 kernel;bootm 0x30007FC0" 怎么实现nand命令读 ...
- linux一周学习总结
对于linux,之前也完全没有接触过,完全零基础小白.来到马哥以后,进入学习也有一周时间 ,一周里老师带我们学习了很多指令,下面,我就自己的理解和老师讲授的内容对linux中的一些指令做一个简单的小总 ...
- 图论中DFS与BFS的区别、用法、详解…
DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...
- 转:【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17965867 方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法 ...
- Java语言课程设计——博客作业教学数据分析系统(201521123107 张翔)
#Java语言课程设计--博客作业教学数据分析系统(个人博客) 1.团队课程设计博客链接 [博客作业教学数据分析系统(From:网络五条狗)](http://www.cnblogs.com/fanta ...