I want to pass an object to an array with the following Class and methods:
class PieData {
constructor(data = [], name, y) {
this.data = data;
this.name = name;
this.y = y;
}
addData() {
this.data.push({
name: this.name,
y: this.y
});
}
}
const browsers = new PieData();
browsers.addData("Chrome", 25);
browsers.addData("Firefox", 30);
console.log(browsers);
The data should look like this:
let data = [
{
name: "Chrome",
y: 61.41
},
{
name: "Internet Explorer",
y: 11.84
}
]
Can anyone help me how to do this? This results in undefined :-(. I´m pretty new to programming. I think I do not really understand the "this" keyword.
browsers.addData("Chrome", 25);but that method doesn't take any parametersthis.dataarray, or is it supposed to be the name of the object? Or something different?