简体   繁体   English

如何使参数中的 javascript class 对象的属性可选?

[英]How can I make javascript class object's attribute in the parameter optional?

I am trying to create a class that doesn't need any parameters but parameters are optional.我正在尝试创建一个不需要任何参数但参数是可选的 class。 The parameters contain a single object that will be restructured.参数包含将被重组的单个 object。 I also want users not to have to reenter the whole options list if they want to override only one of the default options.如果他们只想覆盖默认选项之一,我还希望用户不必重新输入整个选项列表。 Here is the code I reached so far:这是我到目前为止达到的代码:

class A {
  /**
   * @param {number} path The directory path to store at.
   * @param {boolean} timeStamp The value to check if timestamps are needed.
   */
  constructor({ path, timeStamp } = { path: "./", timeStamp: true }) {
    Object.defineProperty(this, "path", { writable: false, value: path });
    Object.defineProperty(this, "timeStamp", { writable: false, value: timeStamp });
  }
}

but this doesn't work as intended.但这并没有按预期工作。 I had in mind something like this:我想到了这样的事情:

const y = new A({ path: "./here", timeStamp: false });
console.log(y.path); // Logs "./here". As expected
console.log(y.timeStamp); // Logs  "false". As expected

const x = new A({ timeStamp: false });
console.log(x.path); // Logs "undefined". Should be the default value  "./"
console.log(x.timeStamp); // Logs "false". As expected

const z = new A();
console.log(z.path); // Logs "./". As expected
console.log(z.timeStamp); // Logs "true". As expected

Thanks in advance.提前致谢。

Edit: I don't want to use many parameters, the class will eventually have many options.编辑:我不想使用很多参数,class 最终会有很多选项。 Here is what I am trying to accomplish: Electron's BrowserWindow documentations这是我要完成的工作: Electron's BrowserWindow documentations

You're initializing the whole object if it is not passed, what you actually wanna do is to initialize the destructured properties with a default value:如果没有通过,您正在初始化整个 object,您实际上想要做的是使用默认值初始化解构属性:

 constructor({ path = "./", timeStamp = true } = { })

Set up the constructor with two parameters with respective default values instead:使用两个具有各自默认值的参数来设置构造函数:

constructor(path = "./", timeStamp = true)

Here is how I would do it这是我会怎么做

const defaultOptions =  { path: "./", timeStamp: true };
class A {
  /**
   * @param {number} path The directory path to store at.
   * @param {boolean} timeStamp The value to check if timestamps are needed.
   */
  constructor(options = { }) {
    const { path, timeStamp } = Object.assign({}, defaultOptions, options);
    Object.defineProperty(this, "path", { writable: false, value: path });
    Object.defineProperty(this, "timeStamp", { writable: false, value: timeStamp });
  }
}

You set an object with default values, and receive an object as parameter which is initialized empty.您将 object 设置为默认值,并接收 object 作为初始化为空的参数。

Use Object.assign to "inherit" the values.使用 Object.assign 来“继承”这些值。 The first parameter is an empty object as it is the target that receives the properties from the other objects.第一个参数是一个空的 object,因为它是从其他对象接收属性的目标。 Set the passed options after the defaults so they override them.在默认值之后设置传递的选项,以便它们覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中,如何创建一个带有可选参数的function? - In JavaScript, how can I create a function with an optional parameter? 如何在 Typescirpt 中制作可选对象参数? - how to make optional object parameter in Typescirpt? 如何在 javascript 中创建字符串对象的值 - how can I make a string object's value in javascript Javascript 类 - 如何使属性成为必需和可选 - Javascript class - how to make properties required and optional 我可以在javascript中的函数参数中使用对象吗? - Can I use an object in function's parameter in javascript? 如何使用javascript在对象数组中使json对象的字段名称具有值? - How can I make my json object's field name a value with in an object array with javascript? Typescript:如果派生类中未传递任何值,如何设置可选参数? - Typescript: How can I set an optional parameter if no value is passed in a derived class? 我可以绕过可选参数而仍然在Javascript中设置rest参数吗? - Can I bypass optional parameters and still set a rest parameter in Javascript? 如何在不使用JavaScript ES6中的构造函数的情况下使用对象文字来创建类的实例? - How can I use an object literal to make an instance of a class without useing the constructor in JavaScript ES6? 如何使 jsonschema 可选? - How can I make jsonschema optional?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM