如何获取JS变量类型

JavaScript08

如何获取JS变量类型,第1张

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较

如何判断js中的类型呢,先举几个例子:

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

alert(typeof a)

------------>string

alert(typeof b)

------------>number

alert(typeof c)

------------>object

alert(typeof d)

------------>object

alert(typeof e)

------------>function

alert(typeof f)

------------>function

其中typeof返回的类型都是字符串形式,需注意,例如:

alert(typeof a == "string")

------------->true

alert(typeof a == String)

--------------->false

另外typeof

可以判断function的类型;在判断除Object类型的对象时比较方便。

判断已知对象类型的方法: instanceof

alert(c instanceof Array)

--------------->true

alert(d instanceof

Date)

alert(f instanceof Function)

------------>true

alert(f instanceof function)

------------>false

注意:instanceof

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

根据对象的constructor判断:

constructor

alert(c.constructor ===

Array) ---------->true

alert(d.constructor === Date)

----------->true

alert(e.constructor ===

Function) ------->true

注意: constructor 在类继承时会出错

eg,

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了

通用但很繁琐的方法: prototype

alert(Object.prototype.toString.call(a) === ‘[object String]’)

------->true

alert(Object.prototype.toString.call(b) === ‘[object Number]’)

------->true

alert(Object.prototype.toString.call(c) === ‘[object Array]’)

------->true

alert(Object.prototype.toString.call(d) === ‘[object Date]’)

------->true

alert(Object.prototype.toString.call(e) === ‘[object Function]’)

------->true

alert(Object.prototype.toString.call(f) === ‘[object Function]’)

------->true

大小写不能写错,比较麻烦,但胜在通用。

通常情况下用typeof

在JS的学习与工作中,搞清楚相关数据类型是基础中的基础,平均一百家公司面试前端工程师的时候九十九家都会问,请你简单举例一下JS的数据类型,于是菜狗我就将对此进行一些自己粗浅的总结为自己留作笔记同时为各位想要学习的同学提供小小的帮助。

首先,我们要明白,在JS中,数据类型分为一下两种:

基础数据类型分为七种

①Number 数字型 (包含所有的整数,浮点数,负数等)

②String 字符串型 (包含任意文本)

③boolean 布尔型 (仅有两种类型,ture与false)

④Undefined 未定义 (仅有一种,undefined)

⑤Null 空/不存在 (仅有一种,null)

⑥Symbol (实现唯一标识)

对此进行简单的举例

⑦BigInt(任意精度整数)(将与ES10中出现)

对此数据类型存在进行简单解释

BigInt 通过数字加n的方法来表示,支持二进制,八进制,十六进制

以下写法结果均为转换为字符串后得而结果,BigInt类型转换字符串后不会再带着n

Ⅰ.通常写法

Ⅱ .十六进制

Ⅲ.八进制(注意区分数字0与字母o)

Ⅳ. 二进制

引用数据类型共有三种

①Array类型 (数组型)

②Object类型 (对象型)

③Function类型 (函数/方法)

以上就是十种JS中的数据类型,如有错误,欢迎指正。

如果本文能帮到你,那么菜狗很开心,大家一起 加油!

js中有5种数据类型:Undefined、Null、Boolean、Number和String。\x0d\x0a还有一种复杂的数据类型Object,Object本质是一组无序的名值对组成的。\x0d\x0aUndefined类型只有一个值,即undefined,使用var声明变量,但是未对初始化的,这个变量就是Undefined类型的,例子:\x0d\x0avar \x0d\x0ai\x0d\x0aalert(i == undefined)//true\x0d\x0avar i与var i = \x0d\x0aundefined这两句是等价的。\x0d\x0a包含Undefined值的变量和未定义的变量是不一样的。\x0d\x0aNull类型也只有一个值:null.null表示一个空对象的指针。\x0d\x0aBoolean类型:只有两个字面量true和false。但是js中多有的变量都可以使用Boolean()函数转换成一个Boolean类型的值。\x0d\x0aNumber类型:整数和浮点数。NaN:Not \x0d\x0aa Number。这个数值用于本来要返回一个数值,但是却未能放回一个数值的情况,以防止报错。例如:1/0 \x0d\x0a返回的就是NaN。NaN的特点:1、任何涉及NaN的操作都会返回NaN。2、NaN对任何值都不相等,包括自己NaN本身。\x0d\x0a针对NaN特性,JS内置了isNaN()函数,来确定数值是不是NaN类型。\x0d\x0aString类型:略\x0d\x0atypeof操作符:对一个变量进行推断变量的类型,可能返回以下字符串:\x0d\x0a"undefined" \x0d\x0a如果这个值,未定义或者为初始化\x0d\x0a"boolean" 布尔值\x0d\x0a"string" 字符串\x0d\x0a"number" 数值\x0d\x0a"object" \x0d\x0a对象\x0d\x0a"function" 函数\x0d\x0a用法:typeof 95 或者 typeof(95)会返回"number".