Jelajahi Sumber

[function.debounce]: use Object.assign to create extended function

mightyplow 7 tahun lalu
induk
melakukan
1766d65a3c
1 mengubah file dengan 7 tambahan dan 8 penghapusan
  1. 7 8
      src/function/debounce.js

+ 7 - 8
src/function/debounce.js

@@ -12,14 +12,13 @@
  */
 export default (fn, timeout = 0) => {
     let timer = null;
-    const abortTimer = () => clearTimeout(timer);
+    const abort = () => clearTimeout(timer);
 
-    const debouncedFunction = (...args) => {
-        abortTimer();
+    // extend function with abort method
+    return Object.assign((...args) => {
+        abort();
         timer = setTimeout(() => fn(...args), timeout)
-    };
-
-    debouncedFunction.abort = abortTimer;
-
-    return debouncedFunction;
+    }, {
+        abort
+    });
 };