if (!this.myPlugin) {
this.myPlugin = {};
}
* 继承
*/
this.myPlugin.inherit = (function () {
var Temp = function () {};
return function (Target, Origin) {
Temp.prototype = Origin.prototype;
Target.prototype = new Temp();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype
}
});
* 混合 对象1 混合到 对象2 得到新的对象
* @param {object} fristObj
* @param {object} secondObj
*/
this.myPlugin.mixin = function (fristObj, secondObj) {
var newObj = {};
for (var prop in secondObj) {
newObj[prop] = secondObj[prop]
}
for (var prop in fristObj) {
if (!prop in secondObj) {
newObj[prop] = fristObj[prop]
}
}
return newObj;
}
* 克隆
* @param {boolean} deep 是否深度克隆
*/
this.myPlugin.clone = function (obj, deep) {
if (Array.isArray(obj)) {
if (deep) {
var newArr = [];
obj.forEach(function (v) {
newArr.push(this.clone(v, deep));
});
return newArr;
} else {
return obj.slice();
}
} else if (obj !== null && typeof obj === "object") {
var newObj = {};
for (var prop in obj) {
if (deep) {
newObj[prop] = this.clone(obj[prop], deep);
} else {
newObj[prop] = obj[prop];
}
}
return newObj;
} else {
return obj;
}
}
* 详细版
* new WeakMap() 额外开辟一个存储空间来存储当前对象和拷贝对象的对应关系
* WeakMap 只接受对象作为键名(null除外),键名所指向的对象,不计入垃圾回收机制
*/
this.myPlugin.Deepfn = (target,hasMap = new WeakMap()) => {
if(target === null || target instanceof HTMLElement || typeof target !== 'object') return target;
if(target instanceof Date) return new Date(target);
if(target instanceof RegExp) return new RegExp(target);
if(hasMap.get(target)) return hasMap.get(target);
let newClone = new target.constructor();
hasMap.set(target,newClone);
Reflect.ownKeys(target).forEach(key => {
newClone[key] = this.Deepfn(target[key],hasMap);
})
return newClone;
}
* 函数防抖
*/
this.myPlugin.debounce = function (callback, time) {
var timer;
return function () {
clearTimeout(timer);
var args = arguments;
timer = setTimeout(function () {
callback.apply(null, args);
}, time)
}
}
* 函数节流
*/
this.myPlugin.throttle = function (callback, time, immediately) {
if (!immediately) {
immediately = true;
}
if (immediately) {
var t;
return function () {
if (immediately) {
if (!t || Date.now() - t >= time) {
callback.apply(null, arguments);
t = Date.now();
}
}
}
} else {
var timer;
return function () {
if (timer) {
return;
}
var args = arguments;
timer = setTimeout(function () {
callback.apply(null, args);
timer = null
}, time)
}
}
}
* 柯里化函数
* 在函数式编程中,柯里化最重要的作用是把多参函数变为单参函数
*/
this.myPlugin.curry = function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
var that = this;
return function () {
var curArgs = Array.from(arguments);
var totalArgs = args.concat(curArgs);
if (totalArgs.length >= fn.length) {
return fn.apply(null, totalArgs)
} else {
totalArgs.unshift(fn);
return that.curry.apply(that, totalArgs);
}
}
}
* 函数管道
*/
this.myPlugin.pipe = function () {
var args = Array.from(arguments);
return function (val) {
args.forEach(function (v) {
var fn = v;
val = fn(val);
});
return val
}
}
* typeof 判断数据类型
*/
this.myPlugin.typeOf = function(target) {
return Object.prototype.toString.call(target).slice(8,-1).toLowerCase();
}