Sfoglia il codice sorgente

refactor: Use native bind function instead of own (#7491)

close #7408
dmitry-vakhnenko 8 anni fa
parent
commit
dc2171a33a
1 ha cambiato i file con 16 aggiunte e 4 eliminazioni
  1. 16 4
      src/shared/util.js

+ 16 - 4
src/shared/util.js

@@ -174,22 +174,34 @@ export const hyphenate = cached((str: string): string => {
 })
 
 /**
- * Simple bind, faster than native
+ * Simple bind polyfill for environments that do not support it... e.g.
+ * PhantomJS 1.x. Technically we don't need this anymore since native bind is
+ * now more performant in most browsers, but removing it would be breaking for
+ * code that was able to run in PhantomJS 1.x, so this must be kept for
+ * backwards compatibility.
  */
-export function bind (fn: Function, ctx: Object): Function {
+function polyfillBind (fn: Function, ctx: Object): Function {
   function boundFn (a) {
-    const l: number = arguments.length
+    const l = arguments.length
     return l
       ? l > 1
         ? fn.apply(ctx, arguments)
         : fn.call(ctx, a)
       : fn.call(ctx)
   }
-  // record original fn length
+
   boundFn._length = fn.length
   return boundFn
 }
 
+function nativeBind (fn: Function, ctx: Object): Function {
+  return fn.bind(ctx)
+}
+
+export const bind = Function.prototype.bind
+  ? nativeBind
+  : polyfillBind
+
 /**
  * Convert an Array-like object to a real Array.
  */