1 | 'use strict';
|
2 | var aFunction = require('../internals/a-function');
|
3 | var isObject = require('../internals/is-object');
|
4 |
|
5 | var slice = [].slice;
|
6 | var factories = {};
|
7 |
|
8 | var construct = function (C, argsLength, args) {
|
9 | if (!(argsLength in factories)) {
|
10 | for (var list = [], i = 0; i < argsLength; i++) list[i] = 'a[' + i + ']';
|
11 |
|
12 | factories[argsLength] = Function('C,a', 'return new C(' + list.join(',') + ')');
|
13 | } return factories[argsLength](C, args);
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 | module.exports = Function.bind || function bind(that /* , ...args */) {
|
19 | var fn = aFunction(this);
|
20 | var partArgs = slice.call(arguments, 1);
|
21 | var boundFunction = function bound(/* args... */) {
|
22 | var args = partArgs.concat(slice.call(arguments));
|
23 | return this instanceof boundFunction ? construct(fn, args.length, args) : fn.apply(that, args);
|
24 | };
|
25 | if (isObject(fn.prototype)) boundFunction.prototype = fn.prototype;
|
26 | return boundFunction;
|
27 | };
|