Skip to content

Commit

Permalink
refactor: Use native bind function instead of own (#7491)
Browse files Browse the repository at this point in the history
close #7408
  • Loading branch information
dima-takoy-zz authored and yyx990803 committed Mar 8, 2018
1 parent 80e650c commit dc2171a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit dc2171a

Please sign in to comment.