One of my friend had a discussion regarding this,
Consider:
var myString = new String("Hello world");
alert(myString["length"]); // 11
myString["simpleExample"] = true;
alert(myString.simpleExample); // true
myString["spaced Example"] = false;
alert(myString["spaced Example"]); // false
Taking all the properties into an array,
var props = ["length","simpleExample","spaced Example"];
now, this will,
for (var i = 0; i < props.length; i++) {
alert(myString[props[i]]);
}
// alerts each property individually: 11 true false
This doesn't work,
for (var i = 0; i < props.length; i++) {
alert(myString.props[i]);
}
// alerts each property individually: 11 true error/nothing
Is there any way to access those properties/keys(having spaces in them) by a dot notation or simply no for array ([ ]) operator is made solely for this?
EDIT:
The question was asked to my friend in an interview, and the interviewer hinted at a solution by using eval, but I am unable to make out that thing.