class Foo {
  #one
  #two
  #three
  #four
  #five
  #six
  #seven
  #eight
  #nine
  #ten
  #eleven
  #twelve
  #thirteen
  #fourteen
  #fifteen
  #sixteen

  constructor(
    one,
    two,
    three,
    four,
    five,
    six,
    seven,
    eight,
    nine,
    ten,
    eleven,
    twelve,
    thirteen,
    fourteen,
    fifteen,
    sixteen
  ) {
    this.#one = one;
    this.#two = two;
    this.#three = three;
    this.#four = four;
    this.#five = five;
    this.#six = six;
    this.#seven = seven;
    this.#eight = eight;
    this.#nine = nine;
    this.#ten = ten;
    this.#eleven = eleven;
    this.#twelve = twelve;
    this.#thirteen = thirteen;
    this.#fourteen = fourteen;
    this.#fifteen = fifteen;
    this.#sixteen = sixteen;
  }
}

这种(反?)模式的解决方案是什么?

推荐答案

对于任何希望使用您的构造函数的人来说,拥有16个参数都不是一件有趣的事情.您在注释中提出的配置对象概念要有趣得多,当然,当您将其与拥有一个具有所有这些属性的Object类型的私有属性的 idea 结合在一起时.然后,您可以使用Object.assign将其更新为用户的首选项:

class Foo {
  #options = {
    one: 1,
    two: 2,
    three: 3,
    four: 4
  }
  constructor(options = {}) {
    Object.assign(this.#options, options);
    console.log(this.#options);
  }
}

let foo = new Foo({three: 3000});

Javascript相关问答推荐

如何避免使用ajax在Vue 3合成API中重定向

Next.js Next/Image图像隐含性有任何类型-如何修复?

当在select中 Select 选项时,我必须禁用不匹配的 Select

单击子元素时关闭父元素(JS)

Angular:动画不启动

康威的生活游戏规则在我的Javascript版本中不起作用.''我做错了什么?

更改JSON中使用AJAX返回的图像的路径

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

Javascript json定制

从包含数百行的表中获取更改后的值(以表单形式发送到后端)的正确方法是什么?

IF语句的计算结果与实际情况相反

Use Location位置成员在HashRouter内为空

使用Nuxt Apollo在Piniastore 中获取产品细节

覆盖加载器页面避免对页面上的元素进行操作

元素字符串长度html

FileReader()不能处理Firefox和GiB文件

背景动画让网站摇摇欲坠

JWT Cookie安全性

为什么我的Navbar.css没有显示在本地主机页面上?

我的NavLink活动类在REACT-ROUTER-V6中出现问题