typeof
1、typeof 对于原始类型来说,除了 null 都可以显示正确的类型
typeof 1 // ‘number’
typeof ‘1’ // ‘string’
typeof undefined // ‘undefined’
typeof true // ‘boolean’
typeof Symbol() // ‘symbol’
2、typeof 对于对象来说,除了函数都会显示 object,所以说 typeof 并不能准确判断变量到底是什么类型
typeof [] // ‘object’
typeof {} // ‘object’
typeof console.log // ‘function’
…
instance of
const Person = function() {}
const p1 = new Person()
p1 instanceof Person // true
var str = ‘hello world’
str instanceof String // false
var str1 = new String(‘hello world’)
str1 instanceof String // true
…
Object.prototype.toString
prototypeconsole.log(Object.prototype.toString.call(123)); //[object Number]
console.log(Object.prototype.toString.call(‘123’)); //[object String]
console.log(Object.prototype.toString.call(undefined)); //[object Undefined]
console.log(Object.prototype.toString.call(true)); //[object Boolean]
console.log(Object.prototype.toString.call({})); //[object Object]
console.log(Object.prototype.toString.call([])); //[object Array]
console.log(Object.prototype.toString.call(function(){})); //[object Function]
console.log(Object.prototype.toString.call(null)); //[[object Null]]
查看数据格式
最新推荐文章于 2024-06-07 10:42:31 发布