JavaScript /构造函数

时间:2018-02-26 23:26:42

标签: javascript

我还在学习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; //[]

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

1 个答案:

答案 0 :(得分:2)

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

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;

相关问题