查看vue2源码:
src\shared\util.js
const _toString = Object.prototype.toString
1. 获取变量的原型
export function toRawType (value): string {
return _toString.call(value).slice(8, -1)
}
2. 判断变量是否是普通对象
export function isPlainObject (obj): boolean {
return _toString.call(obj) === '[object Object]'
}
3. 判断变量是否是正则表达式
export function isRegExp (v: any): boolean {
return _toString.call(v) === '[object RegExp]'
}
4. 判断变量是否是有效的数组索引
export function isValidArrayIndex (val: any): boolean {
const n = parseFloat(String(val))
return n >= 0 && Math.floor(n) === n && isFinite(val)
}
5. 判断变量是否是Promise
export function isPromise (val: any): boolean {
return (
isDef(val) &&
typeof val.then === 'function' &&
typeof val.catch === 'function'
)
}