美文网首页js从0开始学习
js - 10 JS数据类型检测

js - 10 JS数据类型检测

作者: 大怪兽迪迦 | 来源:发表于2019-11-05 12:03 被阅读0次

数据类型检测

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()
    • 可以轻松对应数据类型的转换

相关文章

  • js - 10 JS数据类型检测

    数据类型检测 typeof typeof [val] : 用来检测数据类型的运算符 typeof的局限性 : 基于...

  • 三、JS 进阶--判断 JS 的数据类型

    判断 JS 的数据类型 判断 JS 数据类型,也就是我们说的类型检测,本文提供五种方法,分别是 typeof 运算...

  • 三、JS 进阶--判断 JS 的数据类型

    判断 JS 的数据类型 判断 JS 数据类型,也就是我们说的类型检测,本文提供五种方法,分别是 typeof 运算...

  • Js类型相关总结

    Js的基本数据类型 复杂数据类型 类型检测 typeof 检测检测原理是根据变量存储时低位 instanceof检...

  • 数据类型的检测

    JS中的数据类型检测 typeof 用来检测数据类型的运算符语法:typeof [value]返回结果:首先是一...

  • 原生Js数据类型及操作知识点总结

    原生Js的数据类型总结 1.基本数据类型与其检测方法 String,Undefined,Boolean,null,...

  • JavaScript数据类型检测总结

    在js中,有四种用于检测数据类型的方式,分别是: typeof用来检测数据类型的运算符 instanceof 检...

  • JavaScript数据类型检测总结

    在js中,有四种用于检测数据类型的方式,分别是: typeof用来检测数据类型的运算符 instanceof 检...

  • JS数据类型检测

    之前有讲过JS数据类型,但是怎么判断到底是某个数据是什么数据类型?今天就来讲一下。JS数据类型检测常用的主要有四种...

  • js入门知识点

    * 基础知识 * js的输出方式 * js的组成 * js的命名规范 * 数据类型 * number数据类型 * ...

网友评论

    本文标题:js - 10 JS数据类型检测

    本文链接:https://www.haomeiwen.com/subject/nwusbctx.html