html数据类型判断,JS中判断数据类型的几种方法

1⃣️首先我们来了解一下js中的数据类型

1.基本数据类型:Undefined、Null、Boolean、Number、String(值类型)

2.复杂数据类型:Object(引用类型)

(值类型和引用类型的定义是通过数据在内存中的存储方式来确定的)

1)变量对象与基础数据类型

JavaScript的执行上下文(context)生成之后,会创建一个叫做变量对象的特殊对象,JavaScript的基础数据类型往往都会保存在变量对象中。

2)引用数据类型与堆内存

引用数据类型的值是保存在堆内存中的对象。JavaScript不允许直接访问堆内存中的位置,因此我们不能直接操作对象的堆内存空间。在操作对象时,实际上是在操作对象的引用而不是实际的对象。因此,引用类型的值都是按引用访问的。这里的引用,我们可以粗浅地理解为保存在变量对象中的一个地址,该地址与堆内存的实际值相关联。当我们要访问堆内存中的引用数据类型时,实际上我们首先是从变量对象中获取了该对象的地址引用(或者地址指针),然后再从堆内存中取得我们需要的数据。

举🌰

var a1 = 0; // 变量对象

var a2 = 'this is string'; // 变量对象

var a3 = null; // 变量对象

var b = { m: 20 }; // 变量b存在于变量对象中,{m: 20} 作为对象存在于堆内存中

var c = [1, 2, 3]; // 变量c存在于变量对象中,[1, 2, 3] 作为对象存在于堆内存中

caee6bac457de84b5f52e55220a4a4cc.png

2⃣️判断js中的数据类型有以下几种方法:typeof、instanceof、 constructor、 Object.toString()+call()、 $.type()/jquery.type()

1.typeof

typeof返回的类型都是字符串形式,对于复杂的数据类型。只有是Function时,typeof返回的才是"Function",其它复杂类型返回的都是"Object",需要注意的是null 它的typeof返回的是"Object"

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

typeof a ------------> string

typeof b ------------> number

typeof c ------------> object

typeof d ------------> object

typeof e ------------> function

typeof f ------------> function

2.instanceof

instanceof是判断已知对象类型的方法(instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。)

语法: a instanceof b  就是判断 a 是不是b类型的数据

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

c instanceof Array---------------> true

d instanceof Date---------------> true

f instanceof Function ------------> true

f instanceof function------------> false

3.根据对象的constructor判断

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

c.constructor === Array ----------> true

d.constructor === Date -----------> true

e.constructor === Function -------> true

注意览页些求时是过解些这确如目前例总站回广随: constructor 在类是能览调不页新代些事几求事都时学下是事功过发,解继承时会出错

function A(){};

function B(){};

A.prototype = new B(); //A继承自B

var aObj = new A();

alert(aObj.constructor === B) -----------> true;

alert(aObj.constructor === A) -----------> false;

而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:

alert(aObj instanceof B) ----------------> true;

alert(aObj instanceof B) ----------------> true;

言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:

aobj.constructor = A; //将自己的类赋值给对象的constructor属性

alert(aObj.constructor === A) -----------> true;

alert(aObj.constructor === B) -----------> false; //基类不会报true了;

4.一个繁琐但比较通用方法,使用Object的toString方法+call()

大小写不能写错,比较麻烦,但是它通用

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

Object.toString.call(a) === ‘[object String]'

Object.toString.call(b) === ‘[object Number]'

Object.toString.call(c) === ‘[object Array]'

Object.toString.call(d) === ‘[object Date]'

Object.toString.call(e) === ‘[object Function]'

Object.toString.call(f) === ‘[object Function]'

5.使用jquery里的type方法(jquery.type()/$.type())

使用这个的话别忘了引入js文件

如果对象是undefined或null,则返回相应的“undefined”或“null”。

jQuery.type( undefined ) === "undefined"

jQuery.type() === "undefined"

jQuery.type( window.notDefined ) === "undefined"

jQuery.type( null ) === "null"

如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字

jQuery.type( true ) === "boolean"

jQuery.type( 3 ) === "number"

jQuery.type( "test" ) === "string"

jQuery.type( function(){} ) === "function"

jQuery.type( [] ) === "array"

jQuery.type( new Date() ) === "date"

jQuery.type( new Error() ) === "error" // as of jQuery 1.9

jQuery.type( /test/ ) === "regexp"

其他一切都将返回它的类型“object”。

本文来源于网络:查看 >https://www.cnblogs.com/aaronhan1215/p/6773314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值