猿问

创建空构造函数并在类中初始化数组时出现问题

我之前尝试过在构造函数之后移动数组。我很确定此时我的语法错误,我已经尝试研究了很长一段时间但没有运气。任何帮助,将不胜感激。


class dataService {

  constructor() {

  }


  //I need to be able to initialize this array of objects and create an empty constructor which does nothing


  var data = [ // Error: Construcotr, method, accessor, or property was expected

  name1 = { name: "Mitchell", gender: "male", address: "201 Burns Street East", age: 20, phoneNumber: "905-550-7379" },

  name2 = { name: "Bob", gender: "male", address: "555 Hello Street", age: 25, phoneNumber: "123-456-7891" },

  name3 = { name: "Gillian", gender: "female", address: "457 Baker Street", age: 23, phoneNumber: "555-111-999" }

];


getData(numRecords) {

  numRecords = numRecords || undefined;

  if (numRecords == undefined) {

    return data;

  }

};



}


哈士奇WWW
浏览 110回答 1
1回答

Helenr

嘿,我的朋友,你在如何定义类的属性方面有几个错误,我建议你查看 javascript 类首先你在数组定义上有错误,这是正确的方法:  this.data = [    { name: "Mitchell", gender: "male", address: "201 Burns Street East", age: 20, phoneNumber: "905-550-7379" },    { name: "Bob", gender: "male", address: "555 Hello Street", age: 25, phoneNumber: "123-456-7891" },   { name: "Gillian", gender: "female", address: "457 Baker Street", age: 23, phoneNumber: "555-111-999" } ];其次,您可以在构造函数上初始化数组并使用“this”关键字分配给类。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答