Intent to ship: JavaScript private methods and accessors

300 views
Skip to first unread message

Joyee Cheung

unread,
Mar 19, 2020, 5:06:57 AM3/19/20
to blink-dev

Contact emails



Explainer


https://github.com/tc39/proposal-private-methods/blob/master/README.md


Spec


https://tc39.es/proposal-private-methods/


Summary


Currently, classes can already define private fields that are only accessible from within the class by using names starting with #. They can also define public fields, public methods and public accessors without the #. This feature completes the matrix by making it possible to define private class methods and accessors using the # syntax as well.


class Counter {
  #count = 0;

  get #value() {
    console.log('Getting the value');
    return this.#count;
  }
  #increment() { this.#count++; }

  equals(obj) { return this.#value === obj.#value; }
}


Private methods and accessors are not accessible outside of the class body:


const counter = new Counter();
counter.#increment();  // SyntaxError
const val = counter.#value;  // SyntaxError


And they can only be invoked on objects created from the class that declare them:


counter.equals({});  // TypeError


This intent to ship also includes private static methods and accessors to complement private static fields and public static methods/accessors:


class ClassCounter {
  static #count = 0;
  static get #value() {
    console.log('Getting the value');
    return ClassCounter.#count;
  }
  static #increment() {
    ClassCounter.#count++;
  }
  static equals(obj) { return ClassCounter.#value === obj.#value; }
}

ClassCounter.#value;  // SyntaxError
ClassCounter.#increment();  // SyntaxError
ClassCounter.equals({});  // TypeError


Motivation


To provide a way to encapsulate methods and accessors in classes, and to complement existing class features including private class fields and public methods/accessors.


Debuggability


Private instance methods and accessors on instances can be inspected in the debugger, and private static methods and accessors on classes can be inspected as well. They can also be invoked when the debugger steps into the class bodies.


Interoperability and compatibility risk


The ECMAScript proposal for private methods and accessors reached Stage 3 in TC39 in September 2017. Static class features with privates reached Stage 3 in May 2018.


This feature introduces new syntax that was previously a SyntaxError. The Web compatibility risk is low. Private class fields, which this feature builds on, have been shipped since M72.


Firefox: Public Support

Safari: In development (private methodsprivate accessors)

Edge: No signals


Will this feature be supported on all six Blink platforms (Windows, Mac, Linux, Chrome OS, Android, and Android WebView)?


Yes


Is this feature fully tested?


This feature passes V8’s own mjsunit/cctest tests as well as all the Test262 tests labeled class-methods-private and class-static-methods-private.


Tracking bug


v8:8330


Link to entry on the Chrome Platform Status dashboard


https://www.chromestatus.com/feature/5700509656678400


Requesting approval to ship?


Yes.

Yoav Weiss

unread,
Mar 19, 2020, 7:42:28 AM3/19/20
to Joyee Cheung, blink-dev
Open issues don't count as public support, and this specific issue seems dormant.
Do you know if Mozilla are planning to implement this as well?
(I'm guessing they are not actively-hostile towards the proposal from the fact that it made it to Stage 3)

Safari: In development (private methodsprivate accessors)

That seems to be patches from an unaffiliated WebKit developer, with no comment from the Safari team on whether they are interested in accepting said patches at all, or if they won't live behind a flag if they would land. 

Maybe it's possible to get support statements from them from comments on the TC39 process?
Otherwise, can we ask regarding their plans to follow?

Edge: No signals


Will this feature be supported on all six Blink platforms (Windows, Mac, Linux, Chrome OS, Android, and Android WebView)?


Yes


Is this feature fully tested?


This feature passes V8’s own mjsunit/cctest tests as well as all the Test262 tests labeled class-methods-private and class-static-methods-private.


Tracking bug


v8:8330


Link to entry on the Chrome Platform Status dashboard


https://www.chromestatus.com/feature/5700509656678400


Requesting approval to ship?


Yes.

--
You received this message because you are subscribed to the Google Groups "blink-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/ebcf815c-5d55-4f3d-a8c9-1771f3debe69%40chromium.org.

Yulia Startsev

unread,
Mar 19, 2020, 1:31:20 PM3/19/20
to blink-dev, jo...@igalia.com
Hi Yoav,

Do you know if Mozilla are planning to implement this as well?
(I'm guessing they are not actively-hostile towards the proposal from the fact that it made it to Stage 3)

thanks for asking. I can confirm as the Mozilla delegate to TC39, that we intend to implement it.
To unsubscribe from this group and stop receiving emails from it, send an email to blin...@chromium.org.

sligh...@chromium.org

unread,
Mar 19, 2020, 3:22:14 PM3/19/20
to blink-dev, jo...@igalia.com
LGTM1

Yoav Weiss

unread,
Mar 19, 2020, 3:22:55 PM3/19/20
to Alex Russell, blink-dev, Joyee Cheung
LGTM2

To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/777efc1b-e1e9-41ba-be53-ba793f228622%40chromium.org.

Mike West

unread,
Mar 19, 2020, 3:25:06 PM3/19/20
to Yoav Weiss, Alex Russell, blink-dev, Joyee Cheung

Joyee Cheung

unread,
Mar 19, 2020, 10:27:50 PM3/19/20
to Mike West, Yoav Weiss, Alex Russell, blink-dev
Thanks, we'll wait for the release to be unfrozen to ship.

You received this message because you are subscribed to a topic in the Google Groups "blink-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/a/chromium.org/d/topic/blink-dev/HLpO-HC8aKc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blink-dev+...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/CAKXHy%3DcfXoQB0wkt222gQ540xpch3wRDs%3Dxr89ERAYk9JeA%2Bew%40mail.gmail.com.

ticai...@gmail.com

unread,
Mar 20, 2020, 5:07:01 PM3/20/20
to blink-dev, jo...@igalia.com, msa...@apple.com, Keith Miller
Hi Yoav,

We in Igalia have been working on class features in JSC in close collaboration with Apple reviewers. Based on discussions with the Apple WebKit contributors [1] [2], we were hopeful and positive about their reception of this contribution. In particular, private fields (a dependency of private methods/accessors) are progressing through code review upstream [3]. +cc msaboff and kmiller from JSC for their thoughts. Absent more explicit signals, we could consider this as "No public signals".

BR,
Caio.

To unsubscribe from this group and stop receiving emails from it, send an email to blin...@chromium.org.
Reply all
Reply to author
Forward
0 new messages