Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Private field declarations need to be in constructor scope #25

Closed
erights opened this issue Feb 28, 2016 · 2 comments
Closed

Private field declarations need to be in constructor scope #25

erights opened this issue Feb 28, 2016 · 2 comments

Comments

@erights
Copy link

erights commented Feb 28, 2016

tc39/proposal-class-public-fields#2 applies here as well. Notice the existing notational redundancy in examples such as

class Point {
    #x;
    #y;
    constructor(x = 0, y = 0) {
        this.#x = +x;
        this.#y = +y;
    }
}

If the two lines in the constructor were written using some declare-and-initialize syntax distinct from assignment, then these two lines would be all that is necessary to declare and initialize both of these private fields in a data-dependent way. As with tc39/proposal-class-public-fields#2 , this would smoothly accommodate future support for const private fields, which must be initialized and cannot be assigned.

@erights
Copy link
Author

erights commented Feb 28, 2016

Adapting the notation from wycats/javascript-private-state#11 to this proposal, the syntax for the above example would be

class Point {
    constructor(x = 0, y = 0) {
        private #x = +x;
        private #y = +y;
    }
}

@zenparsing
Copy link
Member

I think we've reached agreement on the location of the definitions within the class body braces.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants