更新时间:2022-06-10 08:32:33
信任的preciously接受的答案居然让我到一点点麻烦。似乎有不只是原型相关的差异更多的事情。我所发现的是,
Trusting the preciously accepted answer actually got me into a little bit of trouble. There seems to be more going on than just prototype-related differences. What I've found is that
的hasOwnProperty不能用,看看是否有键按下present中时关键是引用类型的词典,但在操作员可以。
下面是一个例子来说明。
Here's an example to demonstrate.
code:
var test:Function = function(key:*,label:String):void
{
var d:Dictionary = new Dictionary(true);
d[key] = true;
trace(label);
trace(" hasOwnProperty: " + (d.hasOwnProperty(key)?"true":"false <== !!PROBLEM!!"));
trace(" in: " + (key in d));
trace(" []: " + d[key]);
};
test({}, "indexed by object");
test("string", "key is string");
test(0, "key is number");
test(true, "key is boolean");
结果:
indexed by object
hasOwnProperty: false <== !!PROBLEM!!
in: true
[]: true
key is string
hasOwnProperty: true
in: true
[]: true
key is number
hasOwnProperty: true
in: true
[]: true
key is boolean
hasOwnProperty: true
in: true
[]: true