Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function-bind.js 1.4KB

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