1

i am working with NodeJS project and i would like to access a private variable from its string name, the example i show is not my project but a basic example demonstrating what i want to do.
I tried options such as

this.#[VariableContainingVarName] = ":D"
this.[#VariableContainingVarName] = ":D"

But none of theses worked, i know this is a weird request but there is no security issue with what i am doing, because i filter in a loop before trying to attribute any data to my variable.

class Fun {

  this.#accessible = ":D";

  set coolFunc(varName)
  {   
     console.log(this.#accessible);
     this.~~HOW TO CALL IT HERE~~ = "D:"
     console.log(this.#accessible);

  }
}
var funny = new Fun();
funny.coolFunc("accessible");

Expected output:
:D
D:

----Update----
From what some people suggested just under, this is not a working solution, i want theses variables to be private. Result obtained with following { '#name': 'Button1', '#x': 32, '#y': 32 }

To be more clear i have like 80 variables, they are not forcably called when the controller is being called and they are ALL private. I let the user add their variables with for example

new Fun({
  var1: "val",
  var1: "val2"...
});

Then my constructor call a function that will look for each key and their value to make sure its allowed, and automaticly attribute to the private variable the value user entered. Example of the so said function:

// yes it is private for a reason too
#apply(configAttrib)
{
        for (const [confElemKey, confElemValue] of Object.entries(configAttrib))
    {       
        switch (confElemKey) {
            case 'name':
                if (typeof(confElemValue) !== 'string')
                    throw Error(`Setting ${confElemKey} is expected to be of type 'string'. Got type ${typeof(confElemValue)}.`);
            break;
        }
        // I want to attribute confElemValue to confElemKey here
      
    }
}
xxxxooxaw
  • 43
  • 6

1 Answers1

1

i would like to access a private variable from its string name

This is not possible. The only way to access a private class fields is via dot notation (obj.#privateField) and the #privateField part is always evaluated literally.

Since you are not providing a lot of specific information, maybe a possible solution would be to store an object in a private field and access those object's properties dynamically:

class Fun {

  #data = {accessible: ":D"};

  coolFunc(varName) {   
     console.log(this.#data.accessible);
     this.#data[varName] = "D:"
     console.log(this.#data.accessible);

  }
}
var funny = new Fun();
funny.coolFunc("accessible");
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Hey, i added my code example (currently working on) I was working with an object containing my variables but its not really a proper way to work with i think so i would like to get my private directly into my class not stored in an object (if possible ? i hope so) – xxxxooxaw Mar 18 '22 at 14:04
  • 1
    Well, you can't access private fields dynamically so you either have to give that up or you have to store your data in a way that can be dynamically accessed. There are not a lot of options here. – Felix Kling Mar 18 '22 at 14:06
  • This answer is an ingenious workaround! Thank you! I used it in p5play's SpriteAnimations class. – quinton-ashley Feb 22 '23 at 04:26