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 的类型判断函数? #81

Open
LuckyWinty opened this issue Nov 8, 2020 · 2 comments
Open

如何封装一个 javascript 的类型判断函数? #81

LuckyWinty opened this issue Nov 8, 2020 · 2 comments
Labels

Comments

@LuckyWinty
Copy link
Owner

No description provided.

@LuckyWinty LuckyWinty added the js label Nov 8, 2020
@LuckyWinty
Copy link
Owner Author

LuckyWinty commented Nov 8, 2020

function getType(value) { 
  // 判断数据是 null 的情况 
  if (value === null) { 
      return value + ""; 
  }
  // 判断数据是引用类型的情况 
  if (typeof value === "object") { 
    let valueClass = Object.prototype.toString.call(value), 
    type = valueClass.split(" ")[1].split(""); 
    type.pop(); 
    return type.join("").toLowerCase();
  } else { 
    // 判断数据是基本数据类型的情况和函数的情况 
    return typeof value
  }
}

@AdamZero
Copy link

AdamZero commented Mar 3, 2021

处理那一块建议换成 return Object.prototype.toString.call(value).slice(8,-1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants