0

I try to dynamically use class field name, not a just dynamic var

I have a class like:

class Foo{
  constructor(){
    var papper = "just a papper";
    var crumpled_papper = "this is crumpled papper";
  }
}

Then create new object like:

var bar = new Foo();

And somewhere in my code I wanna to get access some "type of papper", but beforehand I don't know crumpled it or not. For crumpling status I have a new var is_crumpling = "", it can be empty double quotes ("") or has value ("crumpling_");

For get acces to my class var and get message I use:

bar.crumpled+"papper"

but JS execute it like bar.undefinedpapper (instead of bar.crumpled_papper or bar.papper) and I understand it, but how in this case i shold dynamicly call class var name?

P.S. eval also does not work, because bar.eval is not a function

  • check more details here about classes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes – Nil Jan 25 '22 at 13:06

0 Answers0