我认为将类方法放入构造函数中会返回<h1>
,但事实并非如此。
而是返回一个对象/类。
为什么它表现为这种方式而不是返回<h1>
元素?
似乎只有这样做才能起作用: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>