javascript - Get the value of an object property dynamically -
this question has reply here:
javascript object: access variable property name string [duplicate] 3 answersi want value of object property. reasons wont go in to, need function abstract can utilize in different scenarios. here's have:
function getpropertyvalue(obj, prop){ homecoming obj.prop; } var obj = { name: "tom", age: 23 } console.log('age '); console.log(getpropertyvalue(obj, "age"));
this returns underfined
because it's looking property "prop" on object, doesn't exist. how property passed prop
, e.g. if "name"
passed prop
obj.name
?
the jsfiddle here
function getpropertyvalue(obj, prop){ homecoming obj[prop]; } var obj = { name: "tom", age: 23 } var name = getpropertyvalue(obj,"name"); var age = getpropertyvalue(obj,"age");
javascript
No comments:
Post a Comment