0

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>
ProEvilz
  • 5,310
  • 9
  • 44
  • 74
  • 2
    You call `this.createText()` but you don't do anything with the returned value. The whole point of a constructor function is to create instances of a particular kind of object. – Pointy Jul 03 '18 at 16:55
  • Even if I add a return statement before `this.createText()` in the constructor, it still returns an object. Is this simply the wrong way of doing it and the latter the correct way? – ProEvilz Jul 03 '18 at 16:57
  • 4
    The constructor cannot return items like that – Get Off My Lawn Jul 03 '18 at 16:57
  • Because constructor are intended to behave like that: return an instance of the class, and nothing else ? (what you do inside it, will only have side effects, but always an instance of the class is returned). – lealceldeiro Jul 03 '18 at 16:59
  • 4
    You should probably take some time to research basic object-oriented principles. – Some programmer dude Jul 03 '18 at 16:59
  • 3
    If you just want a function to build a string, then write a plain function. Again, classes are for creating objects. If that's not what you want, don't do it. – Pointy Jul 03 '18 at 17:00
  • Thanks, but I don't get why you downvote the question. It's a valid question in search of education. – ProEvilz Jul 03 '18 at 17:00
  • @lealceldeiro or an object, any object. – ibrahim mahrir Jul 03 '18 at 17:02
  • 1
    Why do you think that that would be the expected behavior? What documentation led you to that conclusion? What example did you look at to think that this was correct? Your question lacks research. There is nothing supporting your idea that that is what should be returned; but there are lots of documentation on using classes that support the contrary as @GetOffMyLawn answer shows. – zero298 Jul 03 '18 at 17:06
  • Possible duplicate of [What is returned from a constructor?](https://stackoverflow.com/questions/3350215/what-is-returned-from-a-constructor) – Get Off My Lawn Jul 03 '18 at 17:08
  • @zero298 My conclusion had come from the behavior of only using it to set values. I had never used it to call a method and I expected the behavior of it to simply invoke the method and therefor the method return it's own respective data. I intended to use it as a way of _automatically_ invoking it I suppose... – ProEvilz Jul 03 '18 at 17:11

2 Answers2

1

If a constructor function returns nothing, null, or any atomic / non-object value then said value is ignored and the newly created object reference is given back to the caller. For example, a return value of 0 (zero) from a constructor function will be ignored.

Source

Source 2

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

The default conversion from an object to string is [object Object]

Roy G
  • 901
  • 10
  • 25