JS判断对象类型
对于确定JS内置对象类型,JS提供了typeof运算符,该运算符得到的结果为以下6种:number,boolean,string,function,object,undefined.不过对绝大多数对象而言,typeof都返回"object",无法确定具体的类型。我们使用一种函数Object.prototype.toString.call来判断
<script>
var a = 1;
console.log("a:"+typeof a); //number
console.log("a:"+Object.prototype.toString.call(a)); //[object Number]
var b = NaN;
console.log("b:"+typeof b); //number
console.log("b:"+Object.prototype.toString.call(b)); //[object Number]
var c= Number.MIN_VALUE;
console.log("c:"+typeof c); //number
console.log("c:"+Object.prototype.toString.call(c)); //[object Number]
var d= Infinity;
console.log("d:"+typeof d); //number
console.log("d:"+Object.prototype.toString.call(d)); //[object Number]
var e= "pmx";
console.log("e:"+typeof e); //string
console.log("e:"+Object.prototype.toString.call(e)); //[object String]
var f= true;
console.log("f:"+typeof f); //boolean
console.log("f:"+Object.prototype.toString.call(f)); //[object Boolean]
var g= window;
console.log("g:"+typeof g); //object
console.log("g:"+Object.prototype.toString.call(g)); //[object Window]
var h = [1];
console.log("h:"+typeof h); //object
console.log("h:"+Object.prototype.toString.call(h)); //[object Array]
var i = function(){};
console.log("i:" + typeof i); //function
console.log("i:"+Object.prototype.toString.call(i)); //[object Function]
var j = document;
console.log("j:" + typeof j); //object
console.log("j:"+Object.prototype.toString.call(j)); //[object HTMLDocument]
var k = null;
console.log("k:" + typeof k); //object
console.log("k:"+Object.prototype.toString.call(k)); //[object Null]
var l = undefined;
console.log("l:" + typeof l); //undefined
console.log("l:"+Object.prototype.toString.call(l)); //[object Undefined]
var m = eval;
console.log("m:" + typeof m); //function
console.log("m:"+Object.prototype.toString.call(m)); //[object Function]
var n = new Date();
console.log("n:" + typeof n); //object
console.log("n:"+Object.prototype.toString.call(n)); //[object Date]
</script>
将Object.prototype.toString.call的返回值处理下,使得更直观
var Adsame =function(){
};
Adsame.prototype.whichType = function(){
var class2type = {};
var types = "Boolean Number String Function Array Date RegExp Object Error Undefined Null";
types = types.split(" ");
for(var i = 0; i<types.length; i++){
class2type["[object "+types[i]+"]"] = types[i].toLowerCase();
}
var params = arguments;
var params_len = params.length;
var param_arr = Array.prototype.slice.call(params);
var result = [] ;
for(var k = 0; k<params_len; k++){
var type = Object.prototype.toString.call(param_arr[k]);
result.push(class2type[type]);
}
return result.length ? result: "undefined" ;
}
JS中还有个类似的运算符instanceof,它是用来判断某个对象是否是某类型的实例
<script>
var a = function(){}
console.log(a instanceof Object);//true
var b = 1;
console.log(b instanceof Object);//false
var c = "1";
console.log(c instanceof Object);//false
var d = true;
console.log(d instanceof Object);//false
var e = undefined;
console.log(e instanceof Object);//false
var f = null;
console.log(f instanceof Object);//false
var g = [1];
console.log(g instanceof Object);//true
var h = window;
console.log(h instanceof Object);//gte IE 9:true,lt IE 9:false
</script>
可以看到,JS中的5中基本类型number,string,boolean,undefined,null并不是Object的实例。
对于低版本的浏览器认为window对象是dom里面的属性,和JS对象是两码事
JS判断对象类型的更多相关文章
- js 判断对象类型
在企业级的开发中,我们常用 typeof 来判断企业 对象类型:但是 typeof 不能判断 Array 和 null 这里我们使用一个 原型上的 toString方法:请看一下代码: <scr ...
- html5 -js判断undefined类型
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...
- js判断浏览器类型 js判断ie6不执行
js判断浏览器类型 $.browser 对象 $.browser.version 浏览器版本 var binfo = ''; if ($.browser.msie) { binfo = " ...
- js判断undefined类型,undefined,null,NaN的区别
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined 所以自作聪明判断 ...
- js验证对象类型
js验证对象类型 1. Object.prototype.toString.call() 这是最佳解决方案,可以用作通用方式处理.各种类型的判断依据类似于[object Object],替换的是后边的 ...
- js判断undefined类型,undefined,null, 的区别详细解析
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...
- js判断undefined类型
js判断undefined类型 if (reValue== undefined){ alert("undefined"); } 发现判断不出来,最后查了下资料要用ty ...
- Js判断对象是否为空,Js判断字符串是否为空
Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...
- 通过JS判断联网类型和连接状态
通过JS判断联网类型和连接状态 中国的移动网络环境复杂,为了给用户带去更好访问体验,开发者希望能了解用户当前的联网方式,然后给用户一个符合当前网络环境的请求结果. W3C的规范中给出了一个方法来获得现 ...
随机推荐
- TYVJ P1077 有理逼近 Label:坑,tle的好帮手 不懂
描述 对于一个素数P,我们可以用一系列有理分数(分子.分母都是不大于N的自然数)来逼近sqrt(p),例如P=2,N=5的时候:1/1<5/4<4/3<sqrt(2)<3/2& ...
- C#SortedList排序列表怎么样逆序输出
C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...
- CentOS 下用的是lnmp 的包配置Nginx 下的CI伪静态(搞爽了)
server { listen ; server_name cy.com; index index.html index.htm index.php default.html default.htm ...
- JavaScript - 获取高度
网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document.body.offset ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- Asp.Net:Repeater 详情 备用
页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...
- SSH+Oracle10G抛Disabling contextual LOB creation as createClob() m
在使用Oracle10G时候,实体类使用了CLOB字段,结果抛了Disabling contextual LOB creation as createClob() method threw error ...
- 红米note3的wifi断流或假死
红米note3的wifi断流/假死 日常使用note3的时,比如长时间浏览网页,点击一个链接会卡住不动,在等待十几秒之后才恢复.第一反应是不是网络不好?但是这种情况常常出现之后,对比其他的手机,比如价 ...
- PHP 文件系统管理函数与 preg_replace() 函数过滤代码
案例:在带行号的代码至文件 crop.js 中.用两种方法去掉代码前面的行号,带行号的代码片段: 1.$(function(){ 2. //初始化图片区域 3. var myimg = new Ima ...
- C++类型转化分析(1)
仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...