我还在学习JavaScript。无法克服这个问题:

  function Fruits(category, berries, notberries) {
      this.category = category;
      this.berries = [];
      this.notberries = [];
  }

  let f = new Fruits("fresh", ["strawberry", "raspberry"], ["apple", "mango"]);

  console.log(f); // Fruits {category: "fresh", berries: Array(0), notberries: Array(0)}

  f.category;  //"fresh"

  f.berries; //[]


为什么不记录浆果的值,而是返回一个空数组?

最佳答案

您需要将参数分配给适当的属性。



function Fruits(category, berries, notberries) {
  this.category = category;
  this.berries = berries;
  this.notberries = notberries;
}

let f = new Fruits("fresh", ["strawberry", "raspberry"], ["apple", "mango"]);

console.log(f); // Fruits {category: "fresh", berries: Array(0),
f.category;
f.berries;

关于javascript - JavaScript/构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48998747/

10-12 00:01