esnext.array.last-item.js 863 B

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