Skip to content

Commit 66d626d

Browse files
dima-takoy-zzhefeng
authored and
hefeng
committed
refactor: Use native bind function instead of own (vuejs#7491)
close vuejs#7408
1 parent f0f7a18 commit 66d626d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Diff for: src/shared/util.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,34 @@ export const hyphenate = cached((str: string): string => {
174174
})
175175

176176
/**
177-
* Simple bind, faster than native
177+
* Simple bind polyfill for environments that do not support it... e.g.
178+
* PhantomJS 1.x. Technically we don't need this anymore since native bind is
179+
* now more performant in most browsers, but removing it would be breaking for
180+
* code that was able to run in PhantomJS 1.x, so this must be kept for
181+
* backwards compatibility.
178182
*/
179-
export function bind (fn: Function, ctx: Object): Function {
183+
function polyfillBind (fn: Function, ctx: Object): Function {
180184
function boundFn (a) {
181-
const l: number = arguments.length
185+
const l = arguments.length
182186
return l
183187
? l > 1
184188
? fn.apply(ctx, arguments)
185189
: fn.call(ctx, a)
186190
: fn.call(ctx)
187191
}
188-
// record original fn length
192+
189193
boundFn._length = fn.length
190194
return boundFn
191195
}
192196

197+
function nativeBind (fn: Function, ctx: Object): Function {
198+
return fn.bind(ctx)
199+
}
200+
201+
export const bind = Function.prototype.bind
202+
? nativeBind
203+
: polyfillBind
204+
193205
/**
194206
* Convert an Array-like object to a real Array.
195207
*/

0 commit comments

Comments
 (0)