【问题标题】:Create default in constructor function if no argument is passed - javascript如果没有传递参数,则在构造函数中创建默认值 - javascript
【发布时间】:2020-11-23 02:35:39
【问题描述】:

我有以下几点:

class example {
   constructor (arg) {
      this.value = arg.value
   }
   print () {
      console.log(this.value);
   }
}

var new_example = new example  ({
   value: 12,
})

new_example.print();

我如何设置一个默认值,比如 value = 10,这样,如果没有给出参数,它将打印 10。即:

var newer_example = new example ({
})

我尝试过类似的方法:

class example {
   constructor (arg) {
      this.value = (arg.value || 10)
   }
   print () {
      console.log(this.value);
   }
}

还有:

class example {
   constructor (arg) {
      this.value = ("undefined" != arg.value || 10)
   }
   print () {
      console.log(this.value);
   }
}

但找不到任何有用的东西。

【问题讨论】:

  • 第一个解决方案的一个问题是它会在任何时候分配 10 arg.value 是错误的,而不仅仅是未定义。例如,如果 arg.value 为 0 或空字符串,则分配 10,即使您可能需要这些值。
  • 第一种方案的另一个问题是,如果arg未定义,arg.value会抛出异常。
  • 抛出异常是什么意思?
  • 它会抛出异常,因为undefined 没有.value 属性。尝试在控制台中运行undefined.value,您将看到错误消息“无法读取未定义的属性'值'”。

标签: javascript class object arguments default


【解决方案1】:

您可以使用对象解构来获得更简洁的代码:

class example {
    constructor (arg = {['value']:3}) {
        this.value = arg.value
    }
    print () {
        console.log(this.value);
    }
}

print 在创建的对象上调用而不向构造函数传递任何内容,将有控制台输出3

var newer_example = new example()
newer_example.print(); /// Console prints `3`

【讨论】:

  • 为什么使用复杂的对象表达式 { ['value']: 3 } 而不是更简单的等效表达式 { value: 3 }
【解决方案2】:

您可以对参数使用默认值,也可以使用解构和对象属性的进一步默认值来支持new example()new example({})

class example {
   constructor ({ value = 10 } = {}) {
      this.value = value
      // Note: You could also use Object.assign(this, { value }) which
      // can become useful once you have more values in your object
   }
   print () {
      console.log(this.value);
   }
}

【讨论】:

【解决方案3】:

您可以使用内置的arguments 并像这样检查:

class example {
  constructor(arg) {
    this.value = arguments.length > 0 ? arg.value : 10;
  }
  print() {
    console.log(this.value);
  }
}

new example().print();
new example({ value: 12 }).print();
new example({ value: false }).print();
new example({ value: '' }).print();

您也可以(接近您所拥有的)验证 arg 是否已定义:

class example {
  constructor(arg) {
    if (arg && typeof arg.value !== "undefined") {
      this.value = arg.value;
    } else {
      this.value = 10;
    }
  }
  print() {
    console.log(this.value);
  }
}

new example().print();
new example({ value: 12 }).print();

或者,如果不需要所有虚假值,您可以使用速记进行上述操作:

class example {
  constructor(arg) {
    this.value = (arg && typeof arg.value === 'number') ? arg.value : 10;
  }
  print() {
    console.log(this.value);
  }
}

new example().print();
new example({ value: 12 }).print();
new example({ value: 0 }).print();
new example({ value: false }).print(); //value false is lost

【讨论】:

  • 我喜欢你的第二个解决方案。有没有办法让它更短?我有很多价值观。
  • 如果你不需要错误的值,你可以做this.value = (arg && arg.value) || 10;
  • 我需要考虑等于 0 的值
  • @CherryDT 答案要好得多
  • 谢谢,他们的回答很好,我只是想弄清楚它是如何工作的
【解决方案4】:
class example {
   constructor (arg) {
      this.value = (arg.value || 10)
   }
   print () {
      console.log(this.value);
   }
}

此代码对我有用。我认为你的问题在第六行 console.log(value) 这应该是console.log(this.value)(你忘了this.)。常见错误!

【讨论】:

  • 不正确 -- 你不能对arg 的所有值使用arg.value || 10 表达式,因为当arg 为空时,在评估arg.value 时会出现运行时错误- - 尝试对空值使用属性。用最小的改变来解决这个问题将使用?. operator 而不是. -- arg?.value || 10。即使arg 未通过并因此为undefined,或者arg 为空,后者也将起作用。
猜你喜欢
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多