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数据类型的判断 #4

Open
nianxiongdi opened this issue Aug 4, 2019 · 0 comments
Open

js数据类型的判断 #4

nianxiongdi opened this issue Aug 4, 2019 · 0 comments
Labels
learing Learning articles

Comments

@nianxiongdi
Copy link
Owner

function typeOf(obj) {
    /**
     * Object.prototype 属性表示 Object 的原型对象。
     * Object.prototype.toString()  返回对象的字符串表示。
     * call的返回值都是"[object String]", 需要解析出来String,用正则进行匹配, /\[object\s|]/g -- g忽略大小写
     */
    return Object.prototype.toString.call(obj).replace(/\[object\s|]/g, '');

}

const obj = {
    name: 'xiaoyi'
}

console.log( typeOf(123) ); // number
console.log( typeOf('123') ); // String
console.log( typeOf(Symbol(1)) ); // Symbol
console.log( typeOf(undefined) ); // Undefined
console.log( typeOf(null) ); // Null
console.log( typeOf(obj) ); // Object
console.log( typeOf([1,2,3]) ); // Array
@nianxiongdi nianxiongdi added the learing Learning articles label Aug 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
learing Learning articles
Projects
None yet
Development

No branches or pull requests

1 participant