0

I want to write the constructor so that every time the object is called, the CSS properties are created using the name of variable that is assigned to the new class instance, plus a unique string. Like so:

class BigBox{

    constructor(){

        var div_box = document.createElement("div");
        div_box.setAttribute("id", this."_title");
        document.body.appendChild(div_box); 
    }

}


var S1 = new BigBox();

So in the above example, the aim is to set the id to be S1_title, it is not working however. What am I doing wrong?

comp1201
  • 407
  • 3
  • 13

1 Answers1

1

That is a bad idea, it is better to just pass the title to the constructor.

class BigBox{

    constructor(title){

        var div_box = document.createElement("div");
        div_box.setAttribute("id", this."_title");
        document.body.appendChild(div_box); 
    }

}


var S1 = new BigBox("S1");
Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46