0

I have tried moving the array before, after the constructor. I am pretty sure I have the syntax wrong at this point, I have tried researching for quite some time without luck. Any help would be appreciated.

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;
  }
};


}
4
  • Sorry, I do not get it; why can't you set this.data = [ /* your array data */ ] in the constructor? Commented Oct 7, 2020 at 15:07
  • The requirement was to have the constructor do nothing, so I am unsure if I am allowed to create the array there. But at this point, I don't see another way and it works using what you commented, so will see. Commented Oct 7, 2020 at 15:10
  • By the way, you cannot have an array like the one you are trying to code. Either you use an array [{ name: "Mitchell", gender: "male", address: "201 Burns Street East", age: 20, phoneNumber: "905-550-7379" }, { ... }, { ... }] (without the "name1", "name2", "name3" labels) or you use an object { name1: { name: "Mitchell", gender: "male", address: "201 Burns Street East", age: 20, phoneNumber: "905-550-7379" }, name2: { ... }, name3: { ... } }. Commented Oct 7, 2020 at 15:15
  • I assume you are referring to some sort of assignement or exercise; can you share it, please? Commented Oct 7, 2020 at 15:17

1 Answer 1

2

Hey my friend you have several errors on how you define properties of classes, i recommend to you to check out javascript classes

mozilla javscript classes documentation and examples

first you have a mistake on array definition, this is the right way:

  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" }
];

Second you can initialize you array on the constructor and assign to the class with the "this" keyword.

I made a working example for you base on your code:

js online example

Peace, mis perros !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.