Open QuocVi1994 opened 3 years ago
function isObject(v) {
return typeof v === "object" && Array.isArray(v) && v !== null;
}
const _toString = Object.prototype.toString
function isPureObject(v) {
return _toString.call(v) === '[object Object]'
}
console.log(isPureObject(Object.create({}))); // true
function isRegExp(v) {
return _toString.call(v) === '[object RegExp]'
}
console.log(isRegExp(new RegExp()), isRegExp(Object.create(null))); // true false
function toNumber(v) {
const n = parseFloat(v)
return isNaN(n) ? v : n;
}
console.log(toNumber('889'), toNumber('88x9')); // 889 88
function toString(v) {
return v == null
? ''
: Array.isArray(v) || (isPureObject(v) && val.toString === _toString)
? JSON.stringify(v, null , 2)
: String(v)
}
判断值是否为undefined或null