(function () {
"use strict";
const class2type = {};
const getProto = Object.getPrototypeOf;
const toString = class2type.toString;
const hasOwn = class2type.hasOwnProperty;
const fnToString = hasOwn.toString;
const ObjectFunctionString = fnToString.call(Object);
const toType = function toType(obj) {
if (obj == null) return obj + "";
const reg = /^\[object ([a-zA-Z0-9]+)\]$/i;
return typeof obj === "object" || typeof obj === "function" ?
reg.exec(toString.call(obj))[1].toLowerCase() :
typeof obj;
};
const isFunction = function isFunction(obj) {
return typeof obj === "function" && typeof obj.nodeType !== "number";
};
const isWindow = function isWindow(obj) {
return obj != null && obj === obj.window;
};
const isArrayLike = function isArrayLike(obj) {
const length = !!obj && "length" in obj && obj.length,
type = toType(obj);
if (isFunction(obj) || isWindow(obj)) return false;
return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && (length - 1) in obj;
};
const isPlainObject = function isPlainObject(obj) {
const proto, Ctor;
if (!obj || toString.call(obj) !== "[object Object]") return false;
proto = getProto(obj);
if (!proto) return true;
Ctor = hasOwn.call(proto, "constructor") && proto.constructor;
return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString;
};
const isEmptyObject = function isEmptyObject(obj) {
if (obj == null) return false;
const keys = Object.keys(obj);
if (typeof Symbol !== "undefined") {
keys = keys.concat(Object.getOwnPropertySymbols(obj));
}
return keys.length === 0;
};
const isNumeric = function isNumeric(obj) {
const type = toType(obj);
return (type === "number" || type === "string") && !isNaN(obj);
};
const each = function each(obj, callback) {
if (typeof callback !== "function") callback = Function.prototype;
let i = 0,
len,
item,
keys,
key;
if (isArrayLike(obj)) {
len = obj.length;
for (; i < len; i++) {
item = obj[i];
if (callback.call(item, item, i) === false) break;
}
} else {
keys = Object.keys(obj);
if (typeof Symbol !== "undefined") {
keys = keys.concat(Object.getOwnPropertySymbols(obj))
};
for (; i < keys.length; i++) {
key = keys[i];
item = obj[key];
if (callback.call(item, item, key) === false) break;
}
}
return obj;
};
思路1:
const merge = function merge() {
const options, src, copyIsArray,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
if (typeof target === "boolean") {
deep = target;
target = arguments[i] || {};
i++;
}
if (typeof target !== "object" && !isFunction(target)) target = {};
for (; i < length; i++) {
if ((options = arguments[i]) != null) {
each(options, function (copy, name) {
if (target === copy) return;
copyIsArray = Array.isArray(copy);
if (deep && copy && (isPlainObject(copy) || copyIsArray)) {
src = target[name];
if (copyIsArray && !Array.isArray(src)) {
src = [];
} else if (!copyIsArray && !isPlainObject(src)) {
src = {};
}
target[name] = merge(deep, src, copy);
return;
}
target[name] = copy;
});
}
}
return target;
};
思路2:
const merge = function merge() {
let options,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
treated = arguments[length - 1];
if (Array.isArray(treated) && treated.treated) {
length--;
} else {
treated = [];
treated.treated = true;
}
if (typeof target === "boolean") {
deep = target;
target = arguments[i] || {};
i++;
}
if (typeof target !== "object" && !isFunction(target)) target = {};
for (; i < length; i++) {
options = arguments[i];
if (options == null) continue;
if (treated.includes(options)) return options;
treated.push(options);
each(options, function (copy, name) {
let copyIsArray = Array.isArray(copy),
copyIsObject = isPlainObject(copy),
src = target[name],
clone = src;
if (deep && copy && (copyIsArray || copyIsObject)) {
if (copyIsArray && !Array.isArray(clone)) clone = [];
if (copyIsObject && !isPlainObject(clone)) clone = {};
target[name] = merge(deep, clone, copy, treated);
} else if (copy !== undefined) {
target[name] = copy;
}
});
}
return target;
};
const clone = function clone(deep, obj, cache) {
const type, Ctor, copy;
if (typeof deep !== "boolean") {
obj = deep;
deep = false;
}
!Array.isArray(cache) ? cache = [] : null;
if (cache.indexOf(obj) > -1) return obj;
cache.push(obj);
if (obj == null) return obj;
if (!/^(object|function)$/.test(typeof obj)) return obj;
type = toType(obj);
Ctor = obj.constructor;
if (/^(number|string|boolean|regexp|date)$/.test(type)) return new Ctor(obj);
if (type === "error") return new Ctor(obj.message);
if (type === "function") {
return function proxy() {
const params = [].slice.call(arguments);
return obj.apply(this, params);
};
}
if (!isPlainObject(obj) && !Array.isArray(obj)) return obj;
copy = new Ctor();
each(obj, function (value, key) {
if (deep) {
copy[key] = clone(deep, value, cache);
return;
}
copy[key] = value;
});
return copy;
};
const utils = {
toType,
isFunction,
isWindow,
isArrayLike,
isPlainObject,
isEmptyObject,
isNumeric,
each,
merge,
clone,
};
const _$ = window._;
utils.noConflict = function noConflict() {
if (window._ === utils) window._ = _$;
return utils;
};
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = utils;
}
if (typeof window !== "undefined") {
window._ = window.utils = utils;
}
})();