1. if (!this.myPlugin) {
    2. this.myPlugin = {};
    3. }
    4. /**
    5. * 继承
    6. */
    7. this.myPlugin.inherit = (function () {
    8. var Temp = function () {};
    9. return function (Target, Origin) {
    10. Temp.prototype = Origin.prototype;
    11. Target.prototype = new Temp();
    12. Target.prototype.constructor = Target;
    13. Target.prototype.uber = Origin.prototype
    14. }
    15. });
    16. /**
    17. * 混合 对象1 混合到 对象2 得到新的对象
    18. * @param {object} fristObj
    19. * @param {object} secondObj
    20. */
    21. this.myPlugin.mixin = function (fristObj, secondObj) {
    22. // 第一种方法 利用 Object.assign
    23. // return Object.assign({},fristObj,secondObj);
    24. //第二种
    25. var newObj = {};
    26. //复制secondObj的属性
    27. for (var prop in secondObj) {
    28. newObj[prop] = secondObj[prop]
    29. }
    30. //找到fristObj中有但是secondObj中没有的属性
    31. for (var prop in fristObj) {
    32. if (!prop in secondObj) {
    33. newObj[prop] = fristObj[prop]
    34. }
    35. }
    36. return newObj;
    37. }
    38. /**
    39. * 克隆
    40. * @param {boolean} deep 是否深度克隆
    41. */
    42. this.myPlugin.clone = function (obj, deep) {
    43. if (Array.isArray(obj)) {
    44. if (deep) {
    45. //深度克隆
    46. var newArr = [];
    47. obj.forEach(function (v) {
    48. newArr.push(this.clone(v, deep));
    49. });
    50. return newArr;
    51. } else {
    52. return obj.slice(); //复制数组
    53. }
    54. } else if (obj !== null && typeof obj === "object") {
    55. var newObj = {};
    56. for (var prop in obj) {
    57. if (deep) {
    58. //深度克隆
    59. newObj[prop] = this.clone(obj[prop], deep);
    60. } else {
    61. newObj[prop] = obj[prop];
    62. }
    63. }
    64. return newObj;
    65. } else {
    66. //函数 原始类型 直接返回
    67. return obj; //递归出口
    68. }
    69. }
    70. /**
    71. * 详细版
    72. * new WeakMap() 额外开辟一个存储空间来存储当前对象和拷贝对象的对应关系
    73. * WeakMap 只接受对象作为键名(null除外),键名所指向的对象,不计入垃圾回收机制
    74. */
    75. this.myPlugin.Deepfn = (target,hasMap = new WeakMap()) => {
    76. // 如果target 为 null, dome元素 , 以及其他原始类型 都是直接返回处理
    77. if(target === null || target instanceof HTMLElement || typeof target !== 'object') return target;
    78. // 处理 Date 对象 RegExp 对象
    79. if(target instanceof Date) return new Date(target);
    80. if(target instanceof RegExp) return new RegExp(target);
    81. if(hasMap.get(target)) return hasMap.get(target); // 当需要拷贝当前对象时,先去存储空间中找,如果有的话直接返回;
    82. let newClone = new target.constructor(); //创建一个新的克隆对象 或者 深度克隆数组
    83. hasMap.set(target,newClone); //如果存储空间中没有就用set 存入 hasMap里面
    84. // 使用 Reflect.ownKeys 就是防止属性名为 symbol的情况
    85. Reflect.ownKeys(target).forEach(key => {
    86. newClone[key] = this.Deepfn(target[key],hasMap);
    87. })
    88. return newClone;
    89. }
    90. /**
    91. * 函数防抖
    92. */
    93. this.myPlugin.debounce = function (callback, time) {
    94. var timer;
    95. return function () {
    96. clearTimeout(timer); //清除之前的定时器
    97. var args = arguments;
    98. timer = setTimeout(function () {
    99. callback.apply(null, args);
    100. }, time)
    101. }
    102. }
    103. /**
    104. * 函数节流
    105. */
    106. this.myPlugin.throttle = function (callback, time, immediately) {
    107. if (!immediately) {
    108. immediately = true;
    109. }
    110. if (immediately) {
    111. var t;
    112. return function () {
    113. if (immediately) {
    114. //之前没有计时 或 距离上次执行的时间已超过规定的值
    115. if (!t || Date.now() - t >= time) {
    116. callback.apply(null, arguments);
    117. t = Date.now(); //得到的当前时间戳
    118. }
    119. }
    120. }
    121. } else {
    122. var timer;
    123. return function () {
    124. if (timer) {
    125. return;
    126. }
    127. var args = arguments; //利用闭包保存参数数组
    128. timer = setTimeout(function () {
    129. callback.apply(null, args);
    130. timer = null
    131. }, time)
    132. }
    133. }
    134. }
    135. /**
    136. * 柯里化函数
    137. * 在函数式编程中,柯里化最重要的作用是把多参函数变为单参函数
    138. */
    139. this.myPlugin.curry = function (fn) {
    140. var args = Array.prototype.slice.call(arguments, 1);
    141. var that = this;
    142. return function () {
    143. //当前调用的参数
    144. var curArgs = Array.from(arguments);
    145. var totalArgs = args.concat(curArgs);
    146. if (totalArgs.length >= fn.length) {
    147. //参数数量够了
    148. return fn.apply(null, totalArgs)
    149. } else {
    150. totalArgs.unshift(fn);
    151. return that.curry.apply(that, totalArgs);
    152. }
    153. }
    154. }
    155. /**
    156. * 函数管道
    157. */
    158. this.myPlugin.pipe = function () {
    159. var args = Array.from(arguments);
    160. return function (val) {
    161. //第一种方法 使用reduce
    162. // return args.reduce(function(result,fn){
    163. // return fn(result)
    164. // },val)
    165. //第二种
    166. args.forEach(function (v) {
    167. var fn = v;
    168. val = fn(val);
    169. });
    170. return val
    171. }
    172. }
    173. /**
    174. * typeof 判断数据类型
    175. */
    176. this.myPlugin.typeOf = function(target) {
    177. // 详细写法
    178. // let res = Object.prototype.toString.call(target).split(' ')[1];
    179. // res = res.substring(0,res.length - 1).toLowerCase();
    180. // return res;
    181. //简便写法
    182. return Object.prototype.toString.call(target).slice(8,-1).toLowerCase();
    183. }