Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private methods in class fields #901

Closed
morten-krogh opened this issue Feb 28, 2021 · 1 comment
Closed

Private methods in class fields #901

morten-krogh opened this issue Feb 28, 2021 · 1 comment

Comments

@morten-krogh
Copy link

morten-krogh commented Feb 28, 2021

This class illustrates the issue

class A {
    pub = this.#priv;

    #priv() {
        console.log('Inside #priv');
    }
}

There is a private method #priv which is being referenced in the class field initializer for pub.

Transpiling with esbuild from ESNEXT to ES6 gives the following

var _priv, priv_fn;
var A = class {
  constructor() {
    __publicField(this, "pub", __privateMethod(this, _priv, priv_fn));
    _priv.add(this);
  }
};
_priv = new WeakSet();
priv_fn = function() {
  console.log("Inside #priv");
};

The transpiled code gives an error.

The problem is the order of the two lines in the constructor.
The class field initializer

  __publicField(this, "pub", __privateMethod(this, _priv, priv_fn));

runs before the private method is defined

  _priv.add(this);

I would guess the solution is just to make sure all private methods are defined first in the constructor (right after super).
The class fields can then be defined after that.

@evanw evanw closed this as completed in 9023d50 Feb 28, 2021
@evanw
Copy link
Owner

evanw commented Feb 28, 2021

Thanks for the report. This will be fixed in the next release.

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

Successfully merging a pull request may close this issue.

2 participants