如何使用 JavaScript 类 递增和递减对象中的值?

How do I increment and decrement values in an object using JavaScript Classes?

我正在尝试做一个简单的背包 class 来用原版 javascript 查看对象。这样做时,我 运行 进入了一个块,因为我无法创建正确的 AddItem 方法或使我的 decrementItem 方法起作用。

我的代码如下:

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(i, value) {
    return this.items[i] ? (this.items[i] += value) : (this.items[i] = 1);
  }

  decrementItem(i) {
    if (this.items[i]) {
    }
    return this.items[i] ? this.items[i]-- : (this.items[i] = 0);
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);

我也很好奇如何向我的 decrementItem 方法添加验证以检查值是否不会变为负值,因为这会导致一些问题。

您可以添加这些更改:

  • i 通常在循环中用来引用数字索引。目前还不清楚您将它用于项目名称。将此变量命名为 item 可能更有意义。
  • addItem中,当没有这样的项目开始时,你总是添加1,丢弃通过的value。确保添加值
  • addItem中,value似乎是一个可选参数。您可以添加默认值 1
  • 更改数量时,您可以使用此 shorthand 来确保使用先前的值,或者当存在 none 或已经为零时为零:this.items[item] || 0
  • Math.max(n, 0) 将允许您确保没有任何值低于零

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(item, value = 1) { // 1 by default if not provided
    return this.items[item] = (this.items[item] || 0) + value; // Add the value
  }

  decrementItem(item) {
    return this.items[item] = Math.max((this.items[item] || 0) - 1, 0); // 0 minimum
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.decrementItem("pencil");
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);

您必须先将项目保存到构造函数

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(i, value) {
    this.items[i] = value ? value : 1;
  }

  decrementItem(i) {
    if (this.items[i]) {
      this.items[i] = this.items[i] - 1;
    } else {
      console.log(`Item not found !!`);
    }
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

newBackpack.decrementItem("pencil");

console.log(newBackpack.items);

我想这对你的面试来说可能看起来不错

class Backpack {
  constructor(name){
    this.name=name
    this.items={}
    this.removeItem=function(item){
      item=item.toLowerCase()
      if(this.items[item]){delete(this.items[item])}
      else{throw Error("Cannot delete a NON-EXISTING attribute "+item)}
    }
    this.minusItem=function(item,number){
      item=item.toLowerCase()
      if(this.items[item]){this.items[item]-=number||1}
      else{throw Error("Cannot Subtract from a NON-EXISTING attribute "+item)}
    }
    this.addItem=function(item,number){
      item=item.toLowerCase()
      if(this.items[item]){this.items[item]+=number||1}
      else{this.items[item]=number||1}
    }
    this.getCountOfItems=function(item){
      item=item.toLowerCase()
      return this.items[item]||null
    }
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil",3);
newBackpack.addItem("pencil",5);
newBackpack.minusItem("pencil",2);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);
console.log(newBackpack.getCountOfItems("pencil")) //6
console.log(newBackpack.getCountOfItems("lunchbox")) //null representing it not existing

try{newBackpack.minusItem("lunchbox",2)}
catch(err){console.error(err.message)}

try{console.log(newBackpack.removeItem("lunchbox"))}
catch(err){console.error(err.message)}