Strange javascript behavior - js object

I want to encode some kind of state machine with different transitions. But something strange happens when I want to select an item.

var transitions = {
    "on": {
        "false":"true",
        "true":"false"
    }
}

The last two lines are very interesting - the same index, the first hardcodedand the second inside the variable. Why does the first return the correct result (false) and the other undefined?

console.log(attr);                             // on
console.log(transitions[attr]);                // Object { false="true, true="false" }
console.log(current_val);                      // "true"
console.log(typeof current_val);               // string
console.log(transitions[attr]["true"]);        // false
console.log(transitions[attr][current_val]);   // undefined

info: I am using FF 14.0.1

+5
source share
2 answers

Notice what console.log(current_val);displays "true"on the console. Since it console.logdoes not print quotation marks, it should be so that it current_valcontains '"true"'what does not match "true".

+2
source

- , true .

0

All Articles