js classes/private fields

Discussions about the development and maturation of the platform code (UXP).
Warning: may contain highly-technical topics.

Moderators: trava90, athenian200

Kris_88
Board Warrior
Board Warrior
Posts: 1101
Joined: 2021-01-26, 11:18

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Kris_88 » 2025-05-25, 16:06

Moonchild wrote:
2025-05-25, 15:53
This means B's test() can't access function #t from A because it's private.

There is a problem here too.
This example displays an alert window.

Code: Select all

class A {
  constructor() { };
  #t() { alert('bad'); };
};

class B extends A {
  constructor(){ super(); };
  test() { this.#t(); };
};
  
var s = new B();
s.test();

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

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Moonchild » 2025-05-25, 16:07

No, it's instantiated when you construct B. (it becomes part of this)

new B() causes everthing from A to be instantiated from its definition, then anything from B's definition will be instantiated "on top" of that, overwriting anything with the same name. That's how classes work in just about any language that allows overloading.
So if you define the alert in A, it will be instantiated as part of the B instance, and you can call it as part of B just fine. (although you can't access it through super)
"A dead end street is a place to turn around and go into a new direction" - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Kris_88
Board Warrior
Board Warrior
Posts: 1101
Joined: 2021-01-26, 11:18

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Kris_88 » 2025-05-25, 16:21

Moonchild wrote:
2025-05-25, 16:07
No, it's instantiated when you construct B. (it becomes part of this)
The documentation seems to say no.

Okay, think as you wish.
I'm too lazy to argue...

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

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Moonchild » 2025-05-25, 16:27

The documentation says yes.
From the page you linked
Private instance fields

Like their public counterparts, private instance fields:

are added before the constructor runs in a base class, or immediately after super() is invoked in a subclass, and
are only available on instances of the class.

Code: Select all

class ClassWithPrivateField {
  #privateField;

  constructor() {
    this.#privateField = 42;
  }
}

class Subclass extends ClassWithPrivateField {
  #subPrivateField;

  constructor() {
    super();
    this.#subPrivateField = 23;
  }
}

new Subclass(); // In some dev tools, it shows Subclass {#privateField: 42, #subPrivateField: 23}
In some dev tools, it shows Subclass {#privateField: 42, #subPrivateField: 23}
"A dead end street is a place to turn around and go into a new direction" - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Kris_88
Board Warrior
Board Warrior
Posts: 1101
Joined: 2021-01-26, 11:18

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Kris_88 » 2025-05-25, 16:37

Moonchild wrote:
2025-05-25, 16:27
In some dev tools, it shows Subclass {#privateField: 42, #subPrivateField: 23}
Ok.
Try html, same result...
No private property isolation.

Code: Select all

<html>
<body>
<script>
class A {
  constructor() { };
  #t() { alert('bad'); };
};

class B extends A {
  constructor(){ super(); };
  test() { this.#t(); };
};
  
var s = new B();
s.test();
</script>
</body>
</html>

Kris_88
Board Warrior
Board Warrior
Posts: 1101
Joined: 2021-01-26, 11:18

Re: multiple german online banking sites i.e. raibadirekt.de stopped working

Unread post by Kris_88 » 2025-05-25, 16:46

It can be even simpler.
I see an alert.

Code: Select all

<html>
<body>
<script>
class A {
  constructor() { };
  #t() { alert('bad'); };
};

var s = new A();
s.#t();
</script>
</body>
</html>

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

Re: js classes/private fields

Unread post by Moonchild » 2025-05-25, 20:32

Seems private fields aren't exactly private, indeed. However, I was thinking you were relating this to the other issue discussed in the original thread, where there's an inheritance question in instantiated classes with overloads, but I see now this is a different issue.

This needs revisiting, then, to add specific access barriers.
"A dead end street is a place to turn around and go into a new direction" - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

dbsoft
Project Contributor
Project Contributor
Posts: 497
Joined: 2020-02-21, 17:35

Re: js classes/private fields

Unread post by dbsoft » 2025-05-25, 22:30

Pretty sure martok when he implemented this said that they aren't really private in our implementation, they just have special names.

https://repo.palemoon.org/MoonchildProd ... ment-37520

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

Re: js classes/private fields

Unread post by Moonchild » 2025-05-25, 23:14

Right! I recall now. So yes, that's as-expected, then, for our implementation.
Honestly with how little this comes up it really is low priority (as it's probably a PITA to address).
"A dead end street is a place to turn around and go into a new direction" - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite