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) { // 第一种方法 利用 Object.assign // return Object.assign({},fristObj,secondObj); //第二种 var newObj = {}; //复制secondObj的属性 for (var prop in secondObj) { newObj[prop] = secondObj[prop] } //找到fristObj中有但是secondObj中没有的属性 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()) => { // 如果target 为 null, dome元素 , 以及其他原始类型 都是直接返回处理 if(target === null || target instanceof HTMLElement || typeof target !== 'object') return target; // 处理 Date 对象 RegExp 对象 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); //如果存储空间中没有就用set 存入 hasMap里面 // 使用 Reflect.ownKeys 就是防止属性名为 symbol的情况 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) { //第一种方法 使用reduce // return args.reduce(function(result,fn){ // return fn(result) // },val) //第二种 args.forEach(function (v) { var fn = v; val = fn(val); }); return val }}/*** typeof 判断数据类型*/this.myPlugin.typeOf = function(target) { // 详细写法 // let res = Object.prototype.toString.call(target).split(' ')[1]; // res = res.substring(0,res.length - 1).toLowerCase(); // return res; //简便写法 return Object.prototype.toString.call(target).slice(8,-1).toLowerCase();}