{"id":811,"date":"2022-08-30T15:07:34","date_gmt":"2022-08-30T15:07:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/using-a-string-as-an-object-reference-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:34","modified_gmt":"2022-08-30T15:07:34","slug":"using-a-string-as-an-object-reference-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-a-string-as-an-object-reference-collection-of-common-programming-errors\/","title":{"rendered":"Using a string as an object reference-Collection of common programming errors"},"content":{"rendered":"<p>If you&#8217;re talking about the <code>item1<\/code> part, you&#8217;re looking for:<\/p>\n<pre><code>myValue = myTestObject[\"item1\"];\n<\/code><\/pre>\n<p>No need for <code>eval<\/code>. (There almost never is.)<\/p>\n<p>If you&#8217;re talking about getting at the <code>myTestObject<\/code> variable using a &#8220;myTestObject&#8221; string, you want to refactor the code so you&#8217;re not doing that, rather than using <code>eval<\/code>. Unfortunately the variable object used for symbol resolution within the function is not accessible directly. The refactor could just use an object explicitly:<\/p>\n<pre><code>var stringToObjectRef = function() {\n\n    var objects = {};\n\n    var myTestVar = \"myTestObject\";\n    objects.myTestObject = { 'item1' : 100, 'item2' : 12, 'item4' : 18 };\n\n    var myValue = objects[myTestVar].item1;\n\n    alert(myValue);\n\n}();\n<\/code><\/pre>\n<p>Off-topic, I don&#8217;t recall precisely why, but if you&#8217;re going to execute that anonymous function immediately like that, you need to put the function expression in parentheses:<\/p>\n<pre><code>var x = (function() { return 5; })();\n<\/code><\/pre>\n<p>rather than<\/p>\n<pre><code>var x = function() { return 5; }();\n<\/code><\/pre>\n<p>Again, I don&#8217;t recall why, and whether it was because of an implementation bug in a popular interpreter or an actual requirement of the syntax.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:47:03. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re talking about the item1 part, you&#8217;re looking for: myValue = myTestObject[&#8220;item1&#8243;]; No need for eval. (There almost never is.) If you&#8217;re talking about getting at the myTestObject variable using a &#8220;myTestObject&#8221; string, you want to refactor the code so you&#8217;re not doing that, rather than using eval. Unfortunately the variable object used for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-811","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=811"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}