In traditional JavaScript, we can assign a property to the global window
object using a string as the name, like so:
const str = "myVar";
window[str] = "Value";
console.log(myVar);
However, is there a way to achieve the same functionality in Angular 2/4 and Typescript? While we can store variables in components using this.myVar
, can we create a variable with a string as its name? For example:
const str = "myVar";
this[str] = "Value";
// the result should be similar to this.myVar = "Value";