开发者

dynamically select a javascript object attribute

开发者 https://www.devze.com 2023-03-07 01:44 出处:网络
[How] can I achieve the following? var object = new Object(); object.field1 = \"test1\"; object.fiedl2 = \"test2\";

[How] can I achieve the following?

var object = new Object();
object.field1 = "test1";
object.fiedl2 = "test2";

function showSpecificField(fieldName){
    //get the fi开发者_JS百科eld 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"


Use square bracket notation

var baz = 'bar';
foo['bar'] === foo.bar === foo[baz]
0

精彩评论

暂无评论...
验证码 换一张
取 消