let _toString = Object.prototype.toString;
function toRawType (value) {
return _toString.call(value).slice(8, -1);
}
//Special Value
toRawType(null) // "Null"
toRawType(undefined) //"Undefined"
toRawType(NaN) //"Number"
本文介绍了一种使用JavaScript原生方法来检测变量类型的实用技巧。通过重用Object.prototype.toString方法,可以准确地判断出包括特殊值如null和undefined在内的各种变量的具体类型。
let _toString = Object.prototype.toString;
function toRawType (value) {
return _toString.call(value).slice(8, -1);
}
//Special Value
toRawType(null) // "Null"
toRawType(undefined) //"Undefined"
toRawType(NaN) //"Number"

被折叠的 条评论
为什么被折叠?