I have created a constructor function and a few objects that are pushed into an array.
From here I need to call a function on these objects in an array which outputs to a basic html front end. I have created the constructor function and have it all mostly working, however I keep getting an error in dev tools stating my this.render() method shown below is not a function.
I can get this working when I try to just console.log the objects in the array (with a loop), but cannot get past this error of not being able to use render() method. Any help would be appreciated. Code below. Note that the end goal is to have the rendered info appended to a div with id of "output". Thank you
function ArtistType(name, genre, albums) {
this.name = name;
this.genre = genre;
this.albums = albums;
this.publishAlbum = function () {
this.albums++;
};
this.render = function () {
const artistName = document.createElement("h2");
artistName.innerText = this.name;
const genre = document.createElement("div");
genre.innerText = this.genre;
const albums = document.createElement("div");
albums.innerText = this.albums;
document.getElementById("output").appendChild(artistName, genre, albums)
};
}
let artists = [];
const artist1 = new ArtistType("Black Sabbath", "metal", 0)
const artist2 = new ArtistType("Huey Lewis and the News", "rock", 0)
const artist3 = new ArtistType("Beastie Boys", "hip hop", 0)
artists.push(artist1, artist2, artist3);
artist1.publishAlbum();
//below some of what I have tried...
//artists.forEach(this.render())
//have also tried for...in loop and others...example:
//for (var i = 0; i < artists.length; i++) {
// this.render();
}
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Name will go here</h1>
<!-- Output the content -->
<div id="output"></div>
<!-- Link the script -->
<script src="scripts.js"></script>
</body>
</html>