Reputation: 455
is it possible something like this is node.js ?
var LANGUAGE = {
LANG1: 'Language 1',
LANG2 'Language 2'
}
var TARGET = {
TARG1: { LANG1: 'target 1 in lang 1', LANG2: 'target 1 in lang 2' },
TARG2: { LANG1: 'target 2 in lang 1', LANG2: 'target 2 in lang 2' }
}
var ACTION = {
ACT1: { LANG1: 'action 1 in lang 1', LANG2: 'action 1 in lang 2' },
ACT2: { LANG1: 'action 2 in lang 1', LANG2: 'action 2 in lang 2' }
}
var currentLanguage = LANGUAGE.LANG1;
var currentTarget = TARGET.TARG2;
var currentAction = ACTION.ACT1;
var message = 'You have successfully performed ' + currentAction[currentLanguage] + ' on ' + currentTarget[currentLanguage];
console.log(message);
I'm not sure that in the source above there is any relation between LANGUAGE.LANG1 and TARG1: { LANG1: etc...
======== updated with the result after running this in VSCode =======
You have successfully performed undefined on undefined
Upvotes: 0
Views: 4999
Reputation: 455
Thanks to 31piy,
I've found a way it works. Maybe it's not the best one.
var LANGUAGE = {
LANG1: 'Language 1',
LANG2: 'Language 2'
}
var TARGET = {
TARG1: new Map(),
TARG2: new Map()
}
var ACTION = {
ACT1: new Map(),
ACT2: new Map()
}
TARGET.TARG1.set(LANGUAGE.LANG1, 'target 1 in lang 1');
TARGET.TARG1.set(LANGUAGE.LANG2, 'target 1 in lang 2');
TARGET.TARG2.set(LANGUAGE.LANG1, 'target 2 in lang 1');
TARGET.TARG2.set(LANGUAGE.LANG2, 'target 2 in lang 2');
ACTION.ACT1.set(LANGUAGE.LANG1, 'action 1 in lang 1');
ACTION.ACT1.set(LANGUAGE.LANG2, 'action 1 in lang 2');
ACTION.ACT2.set(LANGUAGE.LANG1, 'action 2 in lang 1');
ACTION.ACT2.set(LANGUAGE.LANG2, 'action 2 in lang 2');
var currentLanguage = LANGUAGE.LANG1;
var currentTarget = TARGET.TARG2;
var currentAction = ACTION.ACT1;
var message = 'You have successfully performed ' + currentAction.get(currentLanguage) + ' on ' + currentTarget.get(currentLanguage);
console.log(message);
And the result is as expected :)
You have successfully performed action 1 in lang 1 on target 2 in lang 1
Upvotes: 0
Reputation: 13007
Absolutely, yes. Your problem is that the values in your LANGUAGE object need to be the keys in the other objects, so...
var LANGUAGE = {
LANG1: 'LANG1',
LANG2: 'LANG1'
}
(You're also missing the second colon in the code you posted.)
Using the above prints...
You have successfully performed action 1 in lang 1 on target 2 in lang 1
Upvotes: 2
Reputation: 1934
You should read more about javascript dot and bracket notation.
In your case currentAction[currentLanguage]
will be currentAction['Language 1']
and there is no value with key 'Language 1'
in the currentAction
object (which equals to { LANG1: 'action 1 in lang 1', LANG2: 'action 1 in lang 2' }
)
currentAction['LANG1']
or currentAction.LANG1
will return the value you want.
ES6 has the dynamic key syntax, where you can use a variable for the key in object literal
{ [LANGUAGE.LANG1]: 'some value'}
Upvotes: 2