js - 10 JS数据类型检测

数据类型检测

typeof

typeof [val] : 用来检测数据类型的运算符

console.log(typeof 1)           // => number (返回的是个字符串,包含对应的数据类型)
console.log(typeof null)        // => Object (typeof的局限性,null不是对象)
console.log(typeof [])          // => Object
总结: typeof具有局限性
console.log(typeof typeof typeof [])    
// => 1.typeof [] => "Object"
// => 2.typeof "Objectof" => "String"
// => 3.typeof "String" => "String"
总结: typeof检测的结果都是字符串,所以只要两个及以上同时检测,最后结果必然是“String”

typeof的局限性 : 基于typeof无法细分出当前值是普通对象还是数组对象等,因为只要是对象数据类型,返回的结果都是”Object“


instanceof

instanceof : 用来检测当前实例是否属于某个类,另外,还可以在继承关系中用来判断一个实例是否属于他的父类

  • 判断实例是否属于某个类
var a = new Array();
console.log(a instanceof Array)         // => true
console.log(a instanceof Object)        // => true
1.a属于array类,所以返回true
2.同时,array是Object的子类,因为继承关系得出,a属于Object
function test();
var a = new test();
console.log(a instanceof test)          // => true
  • 判断当前实例是否属于其父类
function Foo(){};
Foo.prototype = new Aoo();  
var foo = new Foo(){};
console.log(foo instanceof Foo)         // => true
console.log(foo instanceof Aoo)         // => true

拓展:prototype是每个对象都有的属性,可以返回对象类型原型,每一个构造函数都有一个属性叫做原型。所以不需要声明,可以直接引用。例:

function test(){};
console.log(test.prototype)         => Object

对于null和undefined,实际上hull所属类就是null,undefined所属类就是undefined,而console时会报错


constructor

constructor : 基于构造函数检测数据类型(也是基于类的方法)

console.log(("1").constructor === String);               // => true
console.log((1).constructor === Number);                 // => true
console.log((true).constructor === Boolean);             // => true
console.log(([]).constructor === Array);                 // => true
console.log((function() {}).constructor === Function);   // => true
console.log(({}).constructor === Object);                // => true

乍一看似乎都能检测出来,但也有特殊情况

function Fn();
Fn.prototypr = new Array();
var f = new Fn();
console.log(f.constructor === Fn)               // => false
console.log(f.constructor === Array)            // => true

声明一个构造函数,并将原型指向Array原型。所以引入第四种方法


Object.prototype.toString.call()

Object.prototype.toString.call() : 检测数据类型的最好方法
jQuery就是依靠这种方法检测数据类型

var a = Object.prototype.toString;

console.log(a.call("aaa"));                 // => [Object String]
console.log(a.call(1));                     // => [Object Number]
console.log(a.call(true));                  // => [Object Boolean]
console.log(a.call(null));                  // => [Object null]
console.log(a.call(undefined));             // => [Object undefined]
console.log(a.call([]));                    // => [Object Array]
console.log(a.call(function() {}));         // => [Object function]
console.log(a.call({}));                    // => [Object Object]

所有数据类型都可以检测出来

function Fn(){};
Fn.prototype = new Array();
Object.prototype.toString.call(Fn);         // => [Object Function]

改动原型也可以检测出来

总结

  • typeof方法:
    • 对于基本数据类型的检测没问题,但是遇到引用数据类型(如:Array)不起作用
  • instanceof方法:
    console.log("1" instanceof String)              // => false
    console.log(new String("1") instanceof String)  // => true
    
  • constructor
    • 声明一个构造函数,并将原型指向Array原型时,会力不从心
  • Object.prototype.toSting.call()
    • 可以轻松对应数据类型的转换
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值