SHARE
TWEET

Variable Variable Names In JavaScript

Phr0zen_Penguin Aug 29th, 2014 215 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [variable_var_names.js]
  4.         How to declare (and manipulate) multiple variables, with variable names, as needed, with the
  5.         added plus of indexing (and accessing) them in a way that standard arrays can't.
  6.  
  7.                 (c) Damion 'Phr0z3n.dev' Tapper, 2014.
  8.                 Email: Phr0z3n.Dev@Gmail.com
  9.   --------------------------------------------------------------------------------------------------
  10.   ==================================================================================================*/
  11.  
  12. /**
  13.  * Consider this line one of the 'gems' of the code that makes 'all things' possible.
  14.  **/
  15. var innerHTMLObject = new Object();
  16.  
  17. function divClearRestore(prefix)
  18. {
  19.         /**
  20.          * From this point forward, the power of concactenation is going to make it possible to
  21.          * (store and) manipulate an 'infinite' numbers of things with very limited coding.
  22.          **/
  23.         if(document.getElementById(prefix + "Button").innerHTML == "Clear")
  24.         {
  25.                 document.getElementById(prefix + "Button").innerHTML = "Restore";
  26.  
  27.                 /**
  28.                  * Consider this line the other 'gem'.  Notice how variables are created with variable
  29.                  * (concactenated) names.  Such a phenomenon is just not possible using the 'var' keyword.
  30.                  **/
  31.                 innerHTMLObject[prefix + "Div"] = document.getElementById(prefix + "Div").innerHTML;
  32.  
  33.                 document.getElementById(prefix + "Div").innerHTML = "";
  34.         }
  35.         else
  36.         {
  37.                 document.getElementById(prefix + "Button").innerHTML = "Clear";
  38.                 document.getElementById(prefix + "Div").innerHTML = innerHTMLObject[prefix + "Div"];
  39.  
  40.                 /**
  41.                  * This line eventually frees the unneeded unused variable.  Such and act adds to the
  42.                  * efficiency and, eventual security of your code.
  43.                  **/
  44.                 delete innerHTMLObject[prefix + "Div"];
  45.         }
  46. }
RAW Paste Data
Top