6
[如何]可以实现以下功能?动态选择javascript对象属性
var object = new Object();
object.field1 = "test1";
object.fiedl2 = "test2";
function showSpecificField(fieldName){
//get the field Name passed in to function from object
//something like: return object.fieldName ???
}
alert(showSpecificField("field2"));// should alert the text "test2"
alert(showSpecificField("field1"));// should alert the text "test1"
WOOT! +5 :) http://jsfiddle.net/kralco626/NHqUu/1/ – kralco626 2011-05-20 11:47:46
谢谢! - 我必须再等10分钟才能接受你的答案,然后我会有“999”代表,这有点酷...虽然能够有1000个会更酷...... – kralco626 2011-05-20 11:49:59
出于好奇,在foo [“bar”]'上面使用'foo.bar'还有什么好处吗? – kralco626 2011-05-20 11:50:58