我之前尝试过在构造函数之后移动数组。我很确定此时我的语法错误,我已经尝试研究了很长一段时间但没有运气。任何帮助,将不胜感激。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; }};}
1 回答
Helenr
TA贡献1780条经验 获得超3个赞
嘿,我的朋友,你在如何定义类的属性方面有几个错误,我建议你查看 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”关键字分配给类。
添加回答
举报
0/150
提交
取消