文章目录
- VUE源码学习 - 01
-
- emptyObject
- isUndef、isDef、isTrue、isFalse
- isPrimitive
- isObject
- _toString、toRawType、isPlainObject、isRegExp
- isValidArrayIndex
- isPromise
- toString、toNumber
- makeMap、isBuiltInTag、isReservedAttribute
- remove
- hasOwn
- cached、camelize、capitalize、hyphenate
- polyfillBind、nativeBind
- toArray
- extend
- toObject
- noop、no、identify
- genStaticKeys
- looseEqual、looseIndexOf
- once
- once
VUE源码学习 - 01
公用工具方法 (src/shared/util.js)
emptyObject
/** 冻结{}对象 */
export const emptyObject = Object.freeze({
});
isUndef、isDef、isTrue、isFalse
/** 是否未被定义,如果参数等于undefined或者为空,返回true */
export function isUndef(v) {
return v === undefined || v === null;
}
/** 是否定义,如果参数不等于undefined或者为空,返回true */
export function isDef(v) {
return v !== undefined && v !== null;
}
/** 是否真,参数为真时返回true */
export function isTrue(v) {
return v === true;
}
/** 是否假,参数为假时返回true */
export function isFalse(v) {
return v === false;
}
isPrimitive
/** 监测value类型是否为 string、number、symbol、boolean */
export function isPrimitive(value) {
return (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'symbol' ||
typeof value === 'boolean'
);
}
isObject
/** 是否为对象,如果对象不等于空且typeof返回为object,返回true */
export function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
_toString、toRawType、isPlainObject、isRegExp
/** 该方法返回描述某个对象数据类型的字符串,
* 如自定义的对象没有被覆盖,则会返回“[object type]”,
* 其中,type则是实际的对象类型。在使用该方法检测的时候,
* 可以使用Object.prototype.toString.call()
* 或者Object.prototype.toString.apply()进行测试 */
var _toString = Object.prototype.toString;
/** 返回value的类型 */
export function toRawType(value) {
return _toString.call(value).slice(8, -1);
}
/** 判断指定参数是否是一个纯粹的对象 */
export function isPlainObject(obj) {
return _toString.call(obj) === '[object Object]';
}
/** 是否是正则表达式 */
export function isRegExp(v) {
return _toString.call(v) === '[object RegExp]';
}
isValidArrayIndex
/** 检查val是否是有效的数组索引值 */
export function isValidArrayIndex(val) {
var n = parseFloat(String(val));
return n >= 0 && Math.floor(n) === n && isFinite(val);
}
isPromise
/** val 是否是Promise函数 */
export function isPromise(val) {
return isDef(val) && typeof val.then === 'function' && typeof val.catch === 'function';
}
toString、toNumber
/** 将val转换为实际呈现的字符串 */
export function toString(val) {