esnext.array.last-item.js 897 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. // TODO: Remove from `core-js@4`
  3. var DESCRIPTORS = require('../internals/descriptors');
  4. var addToUnscopables = require('../internals/add-to-unscopables');
  5. var toObject = require('../internals/to-object');
  6. var lengthOfArrayLike = require('../internals/length-of-array-like');
  7. var defineProperty = require('../internals/object-define-property').f;
  8. // `Array.prototype.lastIndex` accessor
  9. // https://github.com/keithamus/proposal-array-last
  10. if (DESCRIPTORS) {
  11. defineProperty(Array.prototype, 'lastItem', {
  12. configurable: true,
  13. get: function lastItem() {
  14. var O = toObject(this);
  15. var len = lengthOfArrayLike(O);
  16. return len == 0 ? undefined : O[len - 1];
  17. },
  18. set: function lastItem(value) {
  19. var O = toObject(this);
  20. var len = lengthOfArrayLike(O);
  21. return O[len == 0 ? 0 : len - 1] = value;
  22. }
  23. });
  24. addToUnscopables('lastItem');
  25. }