简体   繁体   中英

Using a dynamically created variable to access named object in an object

finalVariables() returns an object that contains data accessible by .dataName notation ie finalVariables().mainViewWindow0 returns the string stored for mainViewWindow0. I'm trying to access mainViewWindow0 using a dynamically created variable, but for obvious reasons this doesn't work so well with dot notation, but I don't know how to work around it. Help to be had for me?

Please ignore the poor coding practice of having a hard-coded number in there; I promise to get rid of it later

 activePane = dot.id.substring(6); //gets dot # and sets it as active pane var tempForPaneNumber = "mainViewWindow" + activePane + ""; document.getElementById("mainViewWindowContent").innerHTML = finalVariables().@@@this is where I want to use the string from "tempForPaneNumber" to access @@@ 

finalVariables[tempForPainNumber]()

如果我理解正确,应该做到这一点。

In Javascript you can access properties of an object either through the dot notation or through the use of brackets to specify the identifier for the property so myVar.foo is equivalent to myVar['foo'] . Therefore, if I understand what you are asking correctly you want to use finalVariables()[tempForPaneNumber]()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM