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

判断 JavaScript 数据类型通用方法 #25

Open
RicoLiu opened this issue Jun 17, 2021 · 0 comments
Open

判断 JavaScript 数据类型通用方法 #25

RicoLiu opened this issue Jun 17, 2021 · 0 comments

Comments

@RicoLiu
Copy link
Owner

RicoLiu commented Jun 17, 2021

function getType(obj) {
  let type = typeof obj;
  if (type !== 'object') {
    return type;
  }

  return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}

> getType()
< "undefined"
> getType([])
< "Array"
> getType({})
<"Object"
> getType(1)
< "number"
> getType('')
< "string"
> getType(window)
< "Window"
> getType(document)
< "HTMLDocument"
> getType(/^a/)
< "RegExp"
> getType(() => {})
< "function"
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