object.assign.es6.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * @file
  3. * Provides a polyfill for Object.assign().
  4. *
  5. * This is needed for Internet Explorer 11 and Opera Mini.
  6. *
  7. * This has been copied from MDN Web Docs code samples. Code samples in the MDN
  8. * Web Docs are licensed under CC0.
  9. *
  10. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
  11. * @see https://developer.mozilla.org/en-US/docs/MDN/About#Code_samples_and_snippets
  12. */
  13. if (typeof Object.assign !== 'function') {
  14. // Must be writable: true, enumerable: false, configurable: true
  15. Object.defineProperty(Object, 'assign', {
  16. value: function assign(target, varArgs) {
  17. // .length of function is 2
  18. 'use strict';
  19. if (target === null || target === undefined) {
  20. throw new TypeError('Cannot convert undefined or null to object');
  21. }
  22. var to = Object(target);
  23. for (var index = 1; index < arguments.length; index++) {
  24. var nextSource = arguments[index];
  25. if (nextSource !== null && nextSource !== undefined) {
  26. for (var nextKey in nextSource) {
  27. // Avoid bugs when hasOwnProperty is shadowed
  28. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  29. to[nextKey] = nextSource[nextKey];
  30. }
  31. }
  32. }
  33. }
  34. return to;
  35. },
  36. writable: true,
  37. configurable: true,
  38. });
  39. }