我想在javascript对象中使用函数作为键.以下工作,至少在Chrome中:
var registry = {};
function Foo(){ };
function Bar(){ };
registry[Foo] = 42;
registry[Bar] = 43;
alert(registry[Foo] + " < " + registry[Bar]);
Run Code Online (Sandbox Code Playgroud)
这是否符合标准?它支持哪些浏览器?
为什么以下代码有效?
var x = []
x[function() {}] = "Hi"
/* prints "Hi" */
console.log(x[function() {}])
Run Code Online (Sandbox Code Playgroud)
编辑
我知道所有东西都被转换为字符串属性,但为什么我可以添加x["function() {}"] = "Yo"并拥有这两个属性?
javascript ×2