Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js typeof 与 Object.prototype.toString.call 比较 #74

Open
gaowei1012 opened this issue Apr 11, 2021 · 0 comments
Open

js typeof 与 Object.prototype.toString.call 比较 #74

gaowei1012 opened this issue Apr 11, 2021 · 0 comments

Comments

@gaowei1012
Copy link
Owner

gaowei1012 commented Apr 11, 2021

typeof 可以对 Number BooleanUndefinedString, Function 类型做比较的,对于对象数组则不适用,数组可以使用 Array 里面的方法,Date, Object, Array, RegExp, Error, Null 使用typeof只会返回 'object'
还可以使用 Object.prototype.toString.call()进行判断,使用如下代码:

const arr = [1, 2, 3]

// 使用typeof 对数组进行判断
console.log(typeof arr) // 'object'

// 对数组进行判断
console.log(Array.isArray([1, 2, 4])) //  true

// 使用 Object  call 对数组进行判断
console.log(Object.prototype.toString.call([1,2,4]))
// 输出 '[object, Array]', 判断是否等于输出值,则可以判断出是否为数组

// 使用 Object call 对对象进行判断
const obj = {}
console.log(Object.prototype.toString.call(obj))
// 输出 '[object, Object]' , 哦按段是否登录输出值

// 使用 Object call 对 字符串、数字、布尔类型

// 字符串
console.log(Object.prototype.toString.call('hello'))
// '[object, String]'

// 数字
console.log(Object.prototype.toString.call(123))
// '[object, Number]'


// 布尔
console.log(Object.prototype.toString.call(false))
// '[object,Boolean]'

image

@gaowei1012 gaowei1012 changed the title js typeof js typeof 与 Object.prototype.toString.call 比较 Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant