[英]Strange Javascript behavior - js object
I want to code some sort 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 interresting - the same index, first hardcoded
and the second stored within a variable. 最后两行是非常有趣的 - 相同的索引,第一个
hardcoded
,第二个存储在变量中。 Why does the first return the right result (false) and the other undefined? 为什么第一个返回正确的结果(false)而另一个未定义?
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 use FF 14.0.1 info:我使用的是FF 14.0.1
Note that console.log(current_val);
注意
console.log(current_val);
outputs "true"
to the console. 输出
"true"
到控制台。 Since console.log
doesn't print quotes, it must be the case that current_val
contains '"true"'
, which isn't the same as "true"
. 由于
console.log
不打印引号,因此必须是current_val
包含'"true"'
,这与"true"
。
这是因为在最后一个语句中,true不会被求值为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.