【问题标题】:JS unnamed class and its unnamed extended classJS无名类及其无名扩展类
【发布时间】:2023-04-08 15:33:01
【问题描述】:

Operation 类创建一个这样的数组,数组前没有类名。

[operInputQuery[0].value, operInputQuery[1].value, operInputQuery[2].value]

'Table' 类旨在成为继承Operation 的构造函数的未命名类。

它可以正确继承,但是,它会像这样使用不必要的标签extends 创建数组。

extends[operInputQuery[0].value, operInputQuery[1].value, operInputQuery[2].value, operInputQuery[3].value]

是的,我不想用“扩展”的东西创建数组。

如何创建一个未命名的扩展类?

let Operation = class { //unamed class
  constructor(a, b, c) {
    this.a = operInputQuery[0].value;
    this.b = operInputQuery[1].value;
    this.c = operInputQuery[2].value;
  }
}

let Table = class extends Operation { //purposed to write an unnmaed extended class
  constructor(a, b, c, d){
    super(a, b, c);
    this.a;
    this.b;
    this.c;
    this.d = operInputQuery[3].value;
    }
};

【问题讨论】:

  • 为什么要在构造函数中传入 a、b、c 并为它们赋值?这里似乎有些不对劲。另外,operInputQuery 是什么?
  • 问题是如何创建未命名的扩展类。
  • 类可以是匿名的,就像函数一样。数组是如何创建的?即使类有一个名字,它也会以某种方式在它前面加上一个名为的类是没有意义的

标签: javascript class unnamed-class


【解决方案1】:

operInputQuery 不见了,但我猜是一个具有 value 属性的对象数组,试试这个:

let operInputQuery = [{value: 1}, {value: 2}, {value: 3}, {value: 4}];

let Table = class { 
  constructor(a, b, c, d){
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
    }
};

let table = new Table(operInputQuery[0].value, operInputQuery[1].value, operInputQuery[2].value, operInputQuery[3].value);
console.log(table);

【讨论】:

  • operInputQuery 是我自己构建的一个函数,这里就不重写代码了,因为它需要html部分。无论如何,感谢您的努力,但您建议创建另一个我不想要的类“表”。
猜你喜欢
  • 1970-01-01
  • 2014-09-18
  • 2012-08-08
  • 2011-05-14
  • 2020-04-28
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多