js基本数据类型以及判断方法

js数据类型:Undefined,Null,Boolean,Number,String(5种基本数据类型),Object(引用类型:Date,Fuction,Array)

es6新增数据类型:Symbol

比较少见且易忽略的类型:BigInt

 

判断bigInt的方法:

1)typeof instance === "bigint"

 

判断symbol的方法:

1)typeof instance === "symbol"

2) Object.prototype.toString.call(Symbol()) // "[object Symbol]"

 

判断undefined的方法:

1)typeof undefined // "undefined"

2)Object.prototype.toString.call(undefined) // "[object Undefined]"

 

判断Null的方法:

1)Object.prototype.toString.call(null) // "[object Null]"

 

判断Boolean的方法:

1)typeof true // "boolean"

2)Object.prototype.toString.call(true) // "[object Boolean]"

 

判断Number的方法:

1)typeof 5 // "number"

2) Object.prototype.toString.call(5) // "[object Number]"

3)!Number.isNaN(5) && !Number.isNaN(parseInt(5) ) 判断本身不是NaN且parseInt之后也不是NaN

 

判断String的方法:

1)typeof 'sss' // "string"

2)Object.prototype.toString.call('sss') // "[object String]"

 

判断Object的方法

1)typeof {} // "object"

2)Object.prototype.toString.call({}) // "[object Object]"

 

判断Array的方法:

1)Array.isArray(arr)

2)Object.prototype.toString.call(arr)==='[object Array]‘’

3)arr instanceof Array 【可以检测出是数组类型,但跨frame实例化的对象彼此是不共享原型链的,所以会出错】

 

判断Function的方法

function cool(){}

1)typeof cool // “function”

2)cool instanceof Function

3)Object.prototype.toString.call(cool) // "[object Function]"

 

方法解析:

1、typeof操作符返回一个字符串,指示未经计算的操作数的类型。

2、instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。(A instanceof B ,检测B.prototype是否在A的原型链上。)

 

MDN参考链接

posted @ 2017-11-21 16:16  真希  阅读(240)  评论(0编辑  收藏  举报