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

数据类型判断 #71

Open
Sunny-117 opened this issue Nov 3, 2022 · 4 comments
Open

数据类型判断 #71

Sunny-117 opened this issue Nov 3, 2022 · 4 comments

Comments

@Sunny-117
Copy link
Owner

Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()

@bearki99
Copy link

function getType(o) {
  const res = Object.prototype.toString.call(o);
  let str = Array.from(res.split(" ")[1]);
  str.pop();
  return str.join('');
}

@QdabuliuQ
Copy link

function getType(data) {
    // [object ]
    let type = Object.prototype.toString.call(data)
    return type.substring(8, type.length-1)
}

@kangkang123269
Copy link

  1. typeof运算符
  2. instanceof运算符
  3. Array.isArray()
  4. Object.prototype.toString.call()
  5. constructor

@QT-7274
Copy link

QT-7274 commented Oct 14, 2023

function getType(obj){
  let type  = typeof obj;
  if (type !== "object") {    // 先进行typeof判断,如果是基础数据类型,直接返回
    return type;
  }
  // 对于typeof返回结果是object的,再进行如下的判断,正则返回结果
  return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1'); 
}

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

5 participants