【发布时间】:2011-07-02 01:19:06
【问题描述】:
我通常通过询问来测试字符串是否是对象中的键:
var foo:Object = {bar:"bar", bah:["bah","bah1"]};
var str:String = "boo";
if(foo[str]) // do something
但是如果 str == "constructor" 它会做一些事情 - 因为 foo["constructor"] 无论如何都会返回 true。
测试字符串是否是对象的键的最佳方法是什么 - 并且对于构造函数不返回 true?
一些例子:
var foo:Object = {bar:"bar", bah:["bah","bah1"]};
trace('foo["bar"]: ' + foo["bar"]);
trace('foo["bah"]: ' + foo["bah"]);
trace('foo["constructor"]: ' + foo["constructor"]);
trace('foo["bar"] == true: ' + (foo["bar"] == true));
trace('foo["bah"] == true: ' + (foo["bah"] == true));
trace('foo["constructor"] == true: ' + (foo["constructor"] == true));
if(foo["bar"]){
trace("foo:bar");
}
if(foo["bah"]){
trace("foo:bah");
}
if(foo["constructor"]){
trace("foo:constructor");
}
trace('"constructor" in foo: ' + ("constructor" in foo));
痕迹:
/*
foo["bar"]: bar
foo["bah"]: bah,bah1
foo["constructor"]: [class Object]
foo["bar"] == true: false
foo["bah"] == true: false
foo["constructor"] == true: false
foo:bar
foo:bah
foo:constructor
"constructor" in foo: true
*/
【问题讨论】:
标签: object actionscript constructor