Node-Red configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

implementation.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. /* eslint no-invalid-this: 1 */
  3. var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ';
  4. var toStr = Object.prototype.toString;
  5. var max = Math.max;
  6. var funcType = '[object Function]';
  7. var concatty = function concatty(a, b) {
  8. var arr = [];
  9. for (var i = 0; i < a.length; i += 1) {
  10. arr[i] = a[i];
  11. }
  12. for (var j = 0; j < b.length; j += 1) {
  13. arr[j + a.length] = b[j];
  14. }
  15. return arr;
  16. };
  17. var slicy = function slicy(arrLike, offset) {
  18. var arr = [];
  19. for (var i = offset || 0, j = 0; i < arrLike.length; i += 1, j += 1) {
  20. arr[j] = arrLike[i];
  21. }
  22. return arr;
  23. };
  24. var joiny = function (arr, joiner) {
  25. var str = '';
  26. for (var i = 0; i < arr.length; i += 1) {
  27. str += arr[i];
  28. if (i + 1 < arr.length) {
  29. str += joiner;
  30. }
  31. }
  32. return str;
  33. };
  34. module.exports = function bind(that) {
  35. var target = this;
  36. if (typeof target !== 'function' || toStr.apply(target) !== funcType) {
  37. throw new TypeError(ERROR_MESSAGE + target);
  38. }
  39. var args = slicy(arguments, 1);
  40. var bound;
  41. var binder = function () {
  42. if (this instanceof bound) {
  43. var result = target.apply(
  44. this,
  45. concatty(args, arguments)
  46. );
  47. if (Object(result) === result) {
  48. return result;
  49. }
  50. return this;
  51. }
  52. return target.apply(
  53. that,
  54. concatty(args, arguments)
  55. );
  56. };
  57. var boundLength = max(0, target.length - args.length);
  58. var boundArgs = [];
  59. for (var i = 0; i < boundLength; i++) {
  60. boundArgs[i] = '$' + i;
  61. }
  62. bound = Function('binder', 'return function (' + joiny(boundArgs, ',') + '){ return binder.apply(this,arguments); }')(binder);
  63. if (target.prototype) {
  64. var Empty = function Empty() {};
  65. Empty.prototype = target.prototype;
  66. bound.prototype = new Empty();
  67. Empty.prototype = null;
  68. }
  69. return bound;
  70. };