support for "Array.prototype.at()"

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
User avatar
szczelnybez
Apollo supporter
Apollo supporter
Posts: 34
Joined: 2021-01-20, 00:11

support for "Array.prototype.at()"

Unread post by szczelnybez » 2022-08-29, 11:40

Hi everyone,

First, my apologies: I couldn't find anything relating to this topic on the forum.
What are the plans on implementing method .at() for javascript in future PM releases? thanks

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35475
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: support for "Array.prototype.at()"

Unread post by Moonchild » 2022-08-29, 11:50

What's wrong with using a normal [index]?
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2183
Joined: 2018-05-05, 13:29

Re: support for "Array.prototype.at()"

Unread post by vannilla » 2022-08-29, 12:19

Moonchild wrote:
2022-08-29, 11:50
What's wrong with using a normal [index]?
Apparently this:
This is not to suggest there is anything wrong with using the square bracket notation. For example array[0] would return the first item. However instead of using array.length for latter items; e.g. array[array.length-1] for the last item, you can call array.at(-1)
(it's the second paragraph in the MDN documentation.)

User avatar
szczelnybez
Apollo supporter
Apollo supporter
Posts: 34
Joined: 2021-01-20, 00:11

Re: support for "Array.prototype.at()"

Unread post by szczelnybez » 2022-08-29, 12:36

Exactly,

This is a functionality that I was always missing. Using .at() you can access last item with LESS code.
Seems trivial, but for example when you access arrays that are objects keys thing gets messy, i.e.
you would have:
object.key1.key2.key3.key4.at(-1)
instead of:
object.key1.key2.key3.key4[ object.key1.key2.key3.key4.length - 1]

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35475
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: support for "Array.prototype.at()"

Unread post by Moonchild » 2022-08-29, 13:09

I still don't see the problem that would require a new access type for something that already exists. But I'm guessing it wouldn't be too difficult to write a convenience function to slot in (something you could actually do in js as well -- just more syntactic sugar...). I'll file an issue for it on the UXP repo.
EDIT: Issue #1996 (UXP) filed
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
szczelnybez
Apollo supporter
Apollo supporter
Posts: 34
Joined: 2021-01-20, 00:11

Re: support for "Array.prototype.at()"

Unread post by szczelnybez » 2022-08-29, 14:40

That's great!
I think this will also improve future compatibility issues.
Thank you Moonchild!

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35475
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: support for "Array.prototype.at()"

Unread post by Moonchild » 2022-08-29, 21:23

I checked the reference material. This is a minor feature listed in ES2022. i.e. people are using it while it's barely been standardized. You should not be using drafts or brand new syntactic sugar functions and expect them to be broadly supported.

In the meantime, this is a reference-compliant implementation in js

Code: Select all

// ES2022 at() method on the built-in indexables
// Array.prototype.at(index)
function ArrayAt(index) {
     // Step 1.
    var O = ToObject(this);
    // Step 2.
    var len = ToLength(O.length);
    // Step 3.
    var relativeIndex = ToInteger(index);
    // Steps 4-5.
    var k;
    if (relativeIndex >= 0) {
        k = relativeIndex;
    } else {
        k = len + relativeIndex;
    }
    // Step 6.
    if (k < 0 || k >= len) {
        return undefined;
    }
    // Step 7.
    return O[k];
}
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
szczelnybez
Apollo supporter
Apollo supporter
Posts: 34
Joined: 2021-01-20, 00:11

Re: support for "Array.prototype.at()"

Unread post by szczelnybez » 2022-08-30, 09:53

Thanks.
Does it mean it will find its way into future PM release soon?

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35475
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: support for "Array.prototype.at()"

Unread post by Moonchild » 2022-08-30, 10:27

szczelnybez wrote:
2022-08-30, 09:53
Does it mean it will find its way into future PM release soon?
Likely in 31.3.0 as the issue has been resolved on the master branch; not only for Array, but also for String and TypedArrays.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 933
Joined: 2021-01-26, 11:18

Re: support for "Array.prototype.at()"

Unread post by Kris_88 » 2022-08-30, 11:31

szczelnybez wrote:
2022-08-29, 14:40
That's great!
I think this will also improve future compatibility issues.
Off-topic:
What is the problem to create your own functions for your convenience without losing compatibility?

Code: Select all

if(!Array.prototype.at) Array.prototype.at = function(ind) { return( this[ (ind<0) ? this.length+ind : ind ] ); };
Instead, each site developer seeks to add his design to the browser engine.
And then we are surprised why the browsers become monstrous, and sites work in only one or two specific browsers. Honestly, I am surprised by the modern trend...

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2183
Joined: 2018-05-05, 13:29

Re: support for "Array.prototype.at()"

Unread post by vannilla » 2022-08-30, 11:52

Kris_88 wrote:
2022-08-30, 11:31
What is the problem to create your own functions for your convenience without losing compatibility?
Because it allows to type less™ and having it in core means it's available everywhere™.
This however completely ignores the fact that except for a handful of people that know what they are doing, current-day web development consists of a number of compatibility layers (babel, etc.) that are always made available during development whose purpose is to implement certain features for browsers that do not have them built-in, thus effectively acting the same as the "standard library" of other languages.
With this approach browser developers would be able to offload the majority of these APIs to these layers and keep in core only the things that can't be implemented in pure Javascript, like operators/syntactic sugar or the whole WebComponents mess.
Unfortunately this would mean not being able to provide new shinies every two weeks, so nobody does it.

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 933
Joined: 2021-01-26, 11:18

Re: support for "Array.prototype.at()"

Unread post by Kris_88 » 2022-08-30, 12:10

vannilla wrote:
2022-08-30, 11:52
Because it allows to type less™ and having it in core means it's available everywhere™.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35475
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: support for "Array.prototype.at()"

Unread post by Moonchild » 2022-08-30, 18:55

vannilla wrote:
2022-08-30, 11:52
having it in core means it's available everywhere™.
Which in turn makes it globally available to be used in attacks/security issues. See also: high-resolution performance timers. Because they were pushed to become a WebAPI to be Available Everywhere™, despite having an extremely limited use case for web pages (it's more of an analysis tool for development than anything else) malicious actors started using them to construct side channel attacks, privacy-infringing checks ("has the user visited domain X before?) etc.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Locked