在ES5 以前,Function.prototype没有bind这个原生方法
    参考 vue 源码中bind兼容

    1. /**
    2. * Simple bind polyfill for environments that do not support it,
    3. * e.g., PhantomJS 1.x. Technically, we don't need this anymore
    4. * since native bind is now performant enough in most browsers.
    5. * But removing it would mean breaking code that was able to run in
    6. * PhantomJS 1.x, so this must be kept for backward compatibility.
    7. */
    8. /* istanbul ignore next */
    9. // 这是Typescript代码,所以参数后面都会有Function等数据类型
    10. function polyfillBind (fn: Function, ctx: Object): Function {
    11. function boundFn (a) {
    12. const l = arguments.length
    13. return l
    14. ? l > 1
    15. ? fn.apply(ctx, arguments)
    16. : fn.call(ctx, a)
    17. : fn.call(ctx)
    18. }
    19. boundFn._length = fn.length
    20. return boundFn
    21. }
    22. function nativeBind (fn: Function, ctx: Object): Function {
    23. return fn.bind(ctx)
    24. }
    25. export const bind = Function.prototype.bind
    26. ? nativeBind
    27. : polyfillBind