小程序类型判断

方法1:Object.prototype.toString.call(x)

console.log(Object.prototype.toString.call(10) == '[object Number]')
console.log(Object.prototype.toString.call(str) == '[object String]')
console.log(Object.prototype.toString.call([1,2,3,4]) == '[object Array]')
console.log(Object.prototype.toString.call({name: 'a'}) == '[object Object]')
console.log(Object.prototype.toString.call(new Date()) == '[object Date]')
console.log(Object.prototype.toString.call(undefined) == '[object Undefined]')
console.log(Object.prototype.toString.call(null) == '[object Null]')

方法2:typeof(x)

console.log(typeof(1) == 'number')
console.log(typeof('1') == 'string')
console.log(typeof([1,2,3]) == 'object')
console.log(typeof({name:'1'}) == 'object')
console.log(typeof(new Date()) == 'object')
console.log(typeof(undefined) == 'undefined')
console.log(typeof(null) == 'object')

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/118707851