javascript - Accessing Object's Data Via Another Variable Name -
it's super late , mind blanking right now, let's have variable filename
, it's storing name of variable marker
. variable marker
array , contains object & property position: new google.maps.latlng(42.2550,-114.3221)
.
i've been stupidly trying access via filename.position
of course returns undefined, since it's searching literal filename
'position' property not exist.
but how pull marker.position
using filename
? there nifty jquery trick for, uh, 'resolving' variable contents? i'm brain fried. know i've done before.
if it's possible in script, can store data not in variable, in property of object (usually it's more convenient use global one). example
var myobj = {}; myobj.marker = new google.maps.latlng(42.2550,-114.3221); // or else
then able property using variable this:
myobj[filename].position
in case recomment check myobj[filename]
existance using typeof
structure, make sure such property exists in myobj
.
if (typeof myobj[filename] !== "undefined") { // }
as apsillers noted, use global window
object well. if marker
variable defined inside other function (i.e. not global), won't able access window.marker
or window[filename]
out of scope.
second way use eval() function i'd recommend avoid.
Comments
Post a Comment