Skip to content

fix(core): Use native bind function instead of own (fix #7408) #7491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 8, 2018
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,28 @@ export const hyphenate = cached((str: string): string => {
})

/**
* Simple bind, faster than native
* Bind function context
*/
export function bind (fn: Function, ctx: Object): Function {
function ownBind (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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's missing the optional arguments, cause different behavior to ownBind

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no optional arguments in ownBind. Are you about fn._length? If yes. It shouldn't be a problem because we should check both length and _length since we don't know exactly was the fn bound via our ownBind or not. e.g.:

(enterHook._length || enterHook.length) > 1

}

export const bind = Function.prototype.bind ? nativeBind : ownBind

/**
* Convert an Array-like object to a real Array.
*/
Expand Down