diff --git a/src/shared/util.js b/src/shared/util.js index a0bc957ee93..3a7664b76e9 100644 --- a/src/shared/util.js +++ b/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. */