I figued putting the class method inside the constructor would return the <h1>
but this is not the case.
Instead it returns an object/the class.
Why does it behave this way instead of returning the <h1>
element?
It seems only doing it this way will it work: new Foo(data).createText();
?
const data = "This is a title";
class Foo {
constructor(data) {
this._title = data;
this.createText();
}
createText() {
return `<h1> ${this._title} </h1>`;
}
}
const targ = document.getElementById('targ');
//Why doesn't this work considering it's called in the constructor?
targ.innerHTML = new Foo(data);
targ.innerHTML += new Foo(data).createText();
<div id="targ"></div>