Proposal: Array.prototype.first() and Array.prototype.last()

段垚 duanyao at ustc.edu
Wed Sep 28 06:59:45 UTC 2016


在 2016/9/28 14:42, Claude Pache 写道:

>
>> Le 28 sept. 2016 à 07:38, 段垚 <duanyao at ustc.edu 
>> <mailto:duanyao at ustc.edu>> a écrit :
>>
>> Because `foo.bar` is equivlant to `foo['bar']` in JS so far, and 
>> `array.-1` could break this consistency.
>>
>>
>> On the other hand, `array.first()` seems not necessary because 
>> `array[0]` is even more handy; `array.last()` looks fine to me.
>>
>>
>> If someone prefer a more general solution, I recommand `array.get(n)`:
>>
>>   * if n >= 0 && n < array.length: equivlant to array[n]
>>   * if n < 0 && -n < array.length: equivlant to array[array.length + n]
>>   * if n <= -array.length || n >= array.length: throw or return undefined
>>   * if n is not a integer or not a number: throw or return undefined
>>
>> The last 2 rules make `array.get(n)` less error prone than 
>> `array[n]`. I prefer throwing, but maybe returning undefined is more 
>> JS-style?
>
> For consistency with the rest of the builtin library, `array.get(n)` 
> should be equivalent to `array.slice(n)[0]`, which means: convert `n` 
> to an integer, and: return `undefined` for out-of-bound index.
I regard such converting behavior a bad legacy of JS, and want to avoid 
it in new APIs.

>
> —Claude
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160928/7467fd17/attachment.html>


More information about the es-discuss mailing list