| | |
| | | 'use strict'; |
| | | var global = require('../internals/global'); |
| | | var uncurryThis = require('../internals/function-uncurry-this'); |
| | | var aCallable = require('../internals/a-callable'); |
| | | var isObject = require('../internals/is-object'); |
| | | var hasOwn = require('../internals/has-own-property'); |
| | | var arraySlice = require('../internals/array-slice'); |
| | | var NATIVE_BIND = require('../internals/function-bind-native'); |
| | | |
| | | var slice = [].slice; |
| | | var Function = global.Function; |
| | | var concat = uncurryThis([].concat); |
| | | var join = uncurryThis([].join); |
| | | var factories = {}; |
| | | |
| | | var construct = function (C, argsLength, args) { |
| | | if (!(argsLength in factories)) { |
| | | if (!hasOwn(factories, argsLength)) { |
| | | for (var list = [], i = 0; i < argsLength; i++) list[i] = 'a[' + i + ']'; |
| | | // eslint-disable-next-line no-new-func -- we have no proper alternatives, IE8- only |
| | | factories[argsLength] = Function('C,a', 'return new C(' + list.join(',') + ')'); |
| | | factories[argsLength] = Function('C,a', 'return new C(' + join(list, ',') + ')'); |
| | | } return factories[argsLength](C, args); |
| | | }; |
| | | |
| | | // `Function.prototype.bind` method implementation |
| | | // https://tc39.es/ecma262/#sec-function.prototype.bind |
| | | module.exports = Function.bind || function bind(that /* , ...args */) { |
| | | var fn = aCallable(this); |
| | | var partArgs = slice.call(arguments, 1); |
| | | module.exports = NATIVE_BIND ? Function.bind : function bind(that /* , ...args */) { |
| | | var F = aCallable(this); |
| | | var Prototype = F.prototype; |
| | | var partArgs = arraySlice(arguments, 1); |
| | | var boundFunction = function bound(/* args... */) { |
| | | var args = partArgs.concat(slice.call(arguments)); |
| | | return this instanceof boundFunction ? construct(fn, args.length, args) : fn.apply(that, args); |
| | | var args = concat(partArgs, arraySlice(arguments)); |
| | | return this instanceof boundFunction ? construct(F, args.length, args) : F.apply(that, args); |
| | | }; |
| | | if (isObject(fn.prototype)) boundFunction.prototype = fn.prototype; |
| | | if (isObject(Prototype)) boundFunction.prototype = Prototype; |
| | | return boundFunction; |
| | | }; |