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
Hobby Astronomer
Hobby Astronomer
Posts: 27
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: 32603
Joined: 2011-08-28, 17:27
Location: Tranås, 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]?
"The best revenge is to not be like the person who wronged you." -- Marcus Aurelius
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb

vannilla
Board Warrior
Board Warrior
Posts: 1967
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
Hobby Astronomer
Hobby Astronomer
Posts: 27
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: 32603
Joined: 2011-08-28, 17:27
Location: Tranås, 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
"The best revenge is to not be like the person who wronged you." -- Marcus Aurelius
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb

User avatar
szczelnybez
Hobby Astronomer
Hobby Astronomer
Posts: 27
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: 32603
Joined: 2011-08-28, 17:27
Location: Tranås, 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];
}
"The best revenge is to not be like the person who wronged you." -- Marcus Aurelius
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb

User avatar
szczelnybez
Hobby Astronomer
Hobby Astronomer
Posts: 27
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: 32603
Joined: 2011-08-28, 17:27
Location: Tranås, 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.
"The best revenge is to not be like the person who wronged you." -- Marcus Aurelius
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb

User avatar
Kris_88
Lunatic
Lunatic
Posts: 302
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
Board Warrior
Board Warrior
Posts: 1967
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
Lunatic
Lunatic
Posts: 302
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: 32603
Joined: 2011-08-28, 17:27
Location: Tranås, 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.
"The best revenge is to not be like the person who wronged you." -- Marcus Aurelius
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb

Post Reply