js变量类型判断 严格通用 Object.prototype.toString.call()

Object.prototype.toString.call()判断结果:

Object.prototype.toString.call(true)
"[object Boolean]"
Object.prototype.toString.call(1)
"[object Number]"
Object.prototype.toString.call(null)
"[object Null]"
Object.prototype.toString.call(undefined)
"[object Undefined]"
Object.prototype.toString.call('ndefined')
"[object String]"
Object.prototype.toString.call([])
"[object Array]"
Object.prototype.toString.call({})
"[object Object]"
Object.prototype.toString.call(alert)
"[object Function]"
Object.prototype.toString.call(/[a-z]/)
"[object RegExp]"

typeof判断结果:

typeof undefined
"undefined"
typeof true
"boolean"
typeof 1
"number"
typeof 'sds'
"string"
typeof alert
"function"
typeof null
"object"
typeof []
"object"
typeof {}
"object"
typeof /[a-z]/
"object"

特别的:(typeof)

new Array(); new Object(); new Boolean(); new RegExp("[a-z]","g");是类似的~

var a = new String('123');
console.log(a);
console.log(Object.prototype.toString.call(a));
console.log(typeof a);
---
var b = new Number(123);
console.log(b);
console.log(Object.prototype.toString.call(b));
console.log(typeof b);

结果为:

String {"123"}
[object String]
object
---
Number {123}
[object Number]
object
posted @ 2018-11-21 16:43  诗和远方-ysk  阅读(274)  评论(0编辑  收藏  举报