您的位置:首页 > Web前端 > JavaScript

js-对象、数组等类型的判断

2018-06-28 00:44 190 查看

一、判断是否为对象

方法一:

var a = new Object();
console.log(a instanceof Object);//true

二、判断是否为数组

方法一:

[code]var b = [];
console.log(Array.isArray(b));
方法二:
var b = [];
console.log(b instanceof Array);

三、其他类型判断
alert(typeof(1));          // number
alert(typeof(NaN));        // number
alert(typeof(Number.MIN_VALUE));  // number
alert(typeof(Infinity));      // number
alert(typeof("123"));       // string
alert(typeof(true));        // boolean
alert(typeof(window));       // object
alert(typeof(document));      // object
alert(typeof(null));        // object
alert(typeof(eval));        // function
alert(typeof(Date));        // function
alert(typeof(sss));        // undefined
alert(typeof(undefined));     // undefined
PS:typeof 无法区分对象和数组,因此返回的结果都是Object

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: