UNPKG

1.46 kBJavaScriptView Raw
1'use strict';
2var uncurryThis = require('../internals/function-uncurry-this');
3var aCallable = require('../internals/a-callable');
4var isObject = require('../internals/is-object');
5var hasOwn = require('../internals/has-own-property');
6var arraySlice = require('../internals/array-slice');
7var NATIVE_BIND = require('../internals/function-bind-native');
8
9var $Function = Function;
10var concat = uncurryThis([].concat);
11var join = uncurryThis([].join);
12var factories = {};
13
14var construct = function (C, argsLength, args) {
15 if (!hasOwn(factories, argsLength)) {
16 var list = [];
17 var i = 0;
18 for (; i < argsLength; i++) list[i] = 'a[' + i + ']';
19 factories[argsLength] = $Function('C,a', 'return new C(' + join(list, ',') + ')');
20 } return factories[argsLength](C, args);
21};
22
23// `Function.prototype.bind` method implementation
24// https://tc39.es/ecma262/#sec-function.prototype.bind
25// eslint-disable-next-line es/no-function-prototype-bind -- detection
26module.exports = NATIVE_BIND ? $Function.bind : function bind(that /* , ...args */) {
27 var F = aCallable(this);
28 var Prototype = F.prototype;
29 var partArgs = arraySlice(arguments, 1);
30 var boundFunction = function bound(/* args... */) {
31 var args = concat(partArgs, arraySlice(arguments));
32 return this instanceof boundFunction ? construct(F, args.length, args) : F.apply(that, args);
33 };
34 if (isObject(Prototype)) boundFunction.prototype = Prototype;
35 return boundFunction;
36};