1.toString 函数
/**
* Convert a value to a string that is actually rendered.
*/
function toString (val) {
return val == null
? ''
: typeof val === 'object'
? JSON.stringify(val, null, 2)
: String(val)
}
2是否是obj, 是否是正则
function isPlainObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}
function isRegExp (v) {
return Object.prototype.toString.call(v) === '[object RegExp]'
}
3.检测数组中的索引是否有效
function isValidArrayIndex (val) {
var n = parseFloat(String(val));
return n >= 0 && Math.floor(n) === n && isFinite(val)
}
4.toNumber 实现
function toNumber (val) {
var n = parseFloat(val);
return isNaN(n) ? val : n
}