0

I have a javascript object (named resource) used for translating my texts.

resource
    .fileNotFound >> "File not found"
    .advSearch >> "Advanced search"

Usually I use this like below:

alert (resource.advSearch);

Now I need to access one of the member of this resource object through a variable.

Example:

var trans = "advSearch";

My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?

Thanks.

Bronzato
  • 9,082
  • 27
  • 113
  • 203

2 Answers2

1

You need to use the bracket notation instead of dot notation as the member operator

resource[trans]
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
0

You can use the [] notation to access properties as well.

resource[trans];
mohkhan
  • 11,649
  • 2
  • 22
  • 27