Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
change .end to .lastItem, add .lastIndex. Closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Mar 19, 2018
1 parent 2819885 commit dcfb925
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ myArray[myIndex - 1] // oops, overcooked index, returns last-but-one not last, s

## High Level Proposal

Array.prototype['end'] is a property with a getter/setter function that returns the last item of the Array.
Array.prototype['lastItem'] is a property with a getter/setter function that returns the last item of the Array.

## Specification

```
22.1.3.xx Array.prototype['end']
22.1.3.xx Array.prototype['lastItem']
It is a property where the attributes are { [[Enumerable]]: false, [[Configurable]]: false, [[Get]]: GetLastArrayItem, [[Set]]: SetLastArrayItem }.
Expand Down Expand Up @@ -55,6 +55,24 @@ When the SetLastArrayItem method is called, the following steps are taken:
Note 1
The SetLastArrayItem function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.
22.1.3.xx Array.prototype['lastIndex']

This comment has been minimized.

Copy link
@ljharb

ljharb Mar 19, 2018

Member

This will have to use dot notation as part of the actual spec text; nbd now ofc but is there any reason not to fix it?

This comment has been minimized.

Copy link
@zloirock

zloirock Mar 19, 2018

Contributor

What is it? Is it just an alias for Array#length? If it's a getter for the last item index, the spec text and polyfill are incorrect.

It is a property where the attributes are { [[Enumerable]]: false, [[Configurable]]: false, [[Get]]: GetLastArrayIndex }.
22.1.3.xx GetLastArrayIndex
When the GetLastArrayIndex method is called, the following steps are taken:
Let O be ? ToObject(this value).
Let len be ? ToLength(? Get(O, "length")).
If len > 0, then
Return len.
Return 0.
Note 1
The GetLastArrayIndex function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.
```

## Polyfill
Expand All @@ -63,7 +81,7 @@ The SetLastArrayItem function is intentionally generic; it does not require that
```js
import { ToString, ToObject, ToLength } from 'es-abstract'
// This polyfill tries to stick as close to the spec as possible. There are polyfills which could use less code.
Object.defineProperty(Array.prototype, 'end', {
Object.defineProperty(Array.prototype, 'lastItem', {
enumerable: false,
configurable: false,
get() {
Expand All @@ -88,14 +106,27 @@ Object.defineProperty(Array.prototype, 'end', {
return O[index] = value
},
})
Object.defineProperty(Array.prototype, 'lastIndex', {
enumerable: false,
configurable: false,
get() {
let O = ToObject(this)
let len = ToLength(O.length)
if (len > 0) {
return len
}
return 0
},
})
```

#### Other considered options

- `Array.prototype.last`/`Array.prototype.last()` was originally proposed but has web-compatibility issues.
- `Array.prototype.end()` as a method. The downside being that setting the last element of an array is still uses awkward syntax.
- `Array.prototype.end` as a getter. However `lastItem` was a more popular name choice.
- `Array.prototype.peek()` was considered (marries well with `push`, `pop`) but `peek` as a setter is unintuitive.
- `Array.prototype.prePop()` [was mentioned](https://github.com/keithamus/proposal-array-last/issues/11#issuecomment-372933559) but suffers from the same issues as `peek` - it is unintuitive for setting, and potentially surprising if it _doesn't_ mutate.
- `Array.prototype.last`/`Array.prototype.last()` was originally proposed but has web-compatibility issues.
- [A bunch of other names](https://github.com/keithamus/proposal-array-last/issues/11#issuecomment-362246040). Most importantly see the following rational for a naming rubric:
- Not have webcompat issues
- Within the top 1000 most popular english words (both last and end are)
Expand Down

0 comments on commit dcfb925

Please sign in to comment.