js 对象中的属性太多

Tyl*_*ler 3 javascript

一个 js 对象中可以有多个属性吗?

我想建立一个类似于workaway.info的网站,并且正在考虑如何设计搜索组件。在高级搜索选项下,他们有大约 30 个用于各种类型工作的复选框。每个工作列表中可以包含多种工作类型。

boolean对象中的每个复选框选项都有一个属性,还是array包含boolean所有选项的输入的属性是典型的吗?

有什么理由选择一个?

    ...
//Method 1:
function Job(theCompany, thePosition){
  this.company = theCompany;
  this.position = thePosition;
  this.isMedical = true;
  this.isAdministrative = true;
  this.isLaw = false;
  ...
  }

//Method 2:
function Job(theCompany, thePosition){
  this.company = theCompany;
  this.position = thePosition;
  this.typeOptions = [true, true, false...];
  ...
  }
Run Code Online (Sandbox Code Playgroud)

Dr.*_*Kay 6

通常,属性限制为 65535。由于您不会达到该限制,因此您的问题纯粹是风格。

我认为这样做的首选方法是拥有一组字符串标志。例如:

function Job(theCompany, thePosition) {
    this.company = theCompany;
    this.position = thePosition;
    this.typeOptions = ['medical', 'administrative'];
}
Run Code Online (Sandbox Code Playgroud)