most of all I need to make this enum caused error if the key is not valid
Unfortunately, you can't. The way you're doing pseudo-enums is the usual way, which is to create properties on an object, but you can't get the JavaScript engine to throw an error if you try to retrieve a property from the object that doesn't exist. Instead, the engine will return undefined
.
You could do this by using a function, which obviously has utility issues.
Alternately (and I really don't like this idea), you could do something like this:
var NAME_FIELD$CAR = "car";
var NAME_FIELD$COLOR = "color";
Since you would access those as free identifiers, trying to read a value that didn't exist:
var temp2 = NAME_FIELD$CAR2;
...fails with a ReferenceError
. (This is true even in non-strict mode code; the Horror of Implicit Globals only applies to writing to a free identifier, not reading from one.)