javascript - 我可以使用变量作为标识符来设置私有(private)类字段吗?如何?

标签 javascript node.js private ecmascript-next class-fields

Node.js 12 支持 private class fields denoted by #开箱即用,没有标志或转译器。

例如,这适用于 Node.js 12:

class Foo {
  #bar = 1;

  constructor({ bar }) {
    this.#bar = bar;
  }

  get bar() {
    return this.#bar;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.bar); // 2

假设我想构造我的 Foo 实例,而不是使用 1 个属性,而是使用 20 个属性——我将不得不在构造函数和 getter 函数中复制赋值语句 20 次,这使得 很多 样板代码。

如果我不使用私有(private)字段而是使用常规类字段,这将不难避免:

class Foo {
  bar = 1;

  constructor(properties) {
    Object.entries(properties).forEach(([name, value]) => (this[name] = value));
  }

  get bar() {
    return this.bar;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.bar); // 2

但是,对于私有(private)类字段,它不起作用:

class Foo {
  #bar = 1;

  constructor(properties) {
    Object.entries(properties).forEach(
      ([name, value]) => (this[`#${name}`] = value)
    );
  }

  get bar() {
    return this.#bar;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.bar); // 1 :-(

我还尝试使用 Reflect.set 为构造函数中的私有(private)类字段赋值,无济于事:

class Foo {
  #bar = 1;

  constructor(properties) {
    Object.entries(properties).forEach(([name, value]) =>
      Reflect.set(this, `#${name}`, value)
    );
  }

  get bar() {
    return this.#bar;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.bar); // 1 :-(

我可以使用变量作为标识符来设置私有(private)类字段吗?如果是,怎么做?

最佳答案

不,这看起来不可能。来自proposal FAQ :

Why doesn't this['#x'] access the private field named #x, given that this.#x does?

  1. This would complicate property access semantics.

  2. Dynamic access to private fields is contrary to the notion of 'private'. E.g. this is concerning:

class Dict extends null {
  #data = something_secret;
  add(key, value) {
    this[key] = value;
  }
  get(key) {
    return this[key];
  }
}
(new Dict).get('#data'); // returns something_secret

语法是这样的,每个私有(private)字段都必须在文字属性名称之前使用 # 进行初始化和/或引用,除此之外别无其他。甚至不允许使用括号符号。

Having a private field named x must not prevent there from being a public field named x, so accessing a private field can't just be a normal lookup.

您甚至不能引用私有(private)字段,除非它在类主体中明确定义(不是类函数,而是直接在类主体中):

class Foo {
  // error is thrown because #abc must be defined in this section
  doSomething() {
    return this.#abc;
  }
}

就是说,没有什么能阻止您创建一个私有(private)属性,它是一个对象,并在该对象上具有所有这些属性:

class Foo {
  #privates = {};

  constructor(properties) {
    Object.entries(properties).forEach(
      ([name, value]) => (this.#privates[name] = value)
    );
  }

  get privates() {
    return this.#privates;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.privates.bar);

关于javascript - 我可以使用变量作为标识符来设置私有(private)类字段吗?如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58297106/

相关文章:

javascript - 我如何将数据从 ReactJS 提交表单传递到 AdonisJS

javascript - Node.js - 将所有流量从端口 A 转发到端口 B

metaprogramming - 您如何从它们所属的类型之外访问私有(private)方法或属性?

java - 为什么外部Java类可以访问内部类私有(private)成员?

javascript - mailto : supported? 有多好

javascript - 通过JS添加类

javascript - 使用 promise 时我仍然会遇到厄运金字塔,我做错了什么?

C# 自动属性来包装私有(private)字段?

javascript - 从本地主机获取 json 数据以在 D3 中使用

javascript - axios响应/返回正在获取数据但需要索引