@babel/plugin-proposal-private-property-in-object

    In

    Out

    1. class Foo {
    2. constructor() {
    3. _bar.set(this, {
    4. writable: true,
    5. value: "bar",
    6. });
    7. test() {
    8. return _bar.has(this);
    9. }
    10. }
    11. var _bar = new WeakMap();
    1. npm install --save-dev @babel/plugin-proposal-private-property-in-object
    1. require("@babel/core").transformSync("code", {
    2. plugins: ["@babel/plugin-proposal-private-property-in-object"],
    3. });

    When true, private property in expressions will check own properties (as opposed to inherited ones) on the object, instead of checking for presence inside a WeakSet. This results in improved performance and debugging (normal property access vs .get()) at the expense of potentially leaking “privates” via things like Object.getOwnPropertyNames.

    Note that both privateFieldsAsProperties and setPublicClassFields must be set to true.

    Example

    In

    1. class Foo {
    2. #bar = "bar";
    3. test(obj) {
    4. }
    5. }
    1. class Foo {
    2. constructor() {
    3. Object.defineProperty(this, _bar, {
    4. writable: true,
    5. value: "bar",
    6. });
    7. }
    8. test() {
    9. return Object.prototype.hasOwnProperty.call(this, _bar);
    10. }
    11. }
    12. var _bar = babelHelpers.classPrivateFieldLooseKey("bar");