html数据类型判断,数据类型判断

var arr = [1,2,3];

var obj = {name: "张三"};

typeof不能判断Array和Object

1.使用isArray()方法

if(Array.isArray){ //判断是否支持该方法

if(Array.isArray(arr)){

console.log("arryes");

}else{

console.log("arrno");

}

if(Array.isArray(obj)){

console.log("objyes");

}else{

console.log("objno");

}

}else{

console.log("不支持该方法")

}

2.instanceof 用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

if (arr instanceof Array) {

console.log("arryes")

}

if (obj instanceof Array) {

console.log("objyes")

}

3.constructor 返回对象的构造函数

if(typeof arr == 'object' && arr.constructor == Array){

console.log("constructor yes");

}

4.prototype 原型对象

if(Object.prototype.toString.call(arr) == '[object Array]'){

console.log("prototype yes");

}

5.__proto__ 原型链

if(arr.__proto__ == Array.prototype){

console.log("__proto__ yes");

}

6.Object.getPrototypeOf()

if(Object.getPrototypeOf(arr) == Array.prototype){

console.log('Object.getPrototypeOf yes');

}

7. Array.prototype.isPrototypeOf

if (Array.prototype.isPrototypeOf(arr)) {

console.log("Array.prototype.isPrototypeOf yes")

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值