class AbstractClass { constructor(variableName) { this.variableName = variableName; } 

At the input I get a string - the name of the variable. Can I then dynamically use variableName to create a new variable as its name? Sort of:

 var this.variableName (в variableName пришло, например, "variable") = "значение", что в итоге стало бы равноценно: var variable = "значение". 

    1 answer 1

    Use parentheses:

    this[this.variableName] = значение

    So you can create any variable in the object with an unknown name in advance.

    If you want to create a global variable, use

    window[this.variableName] = значение .

    You cannot create a local variable this way.

    And yes, to remove such a variable from an object, use:

    delete obj[variableName] - essentially equivalent to obj[variableName] = undefined , but not sure that the second method is optimal in terms of memory costs (or rather, not sure if it will be cleared)