{"id":78,"date":"2008-06-24T09:29:26","date_gmt":"2008-06-24T17:29:26","guid":{"rendered":"http:\/\/www.antunkarlovac.com\/blog\/?p=78"},"modified":"2021-05-07T00:39:15","modified_gmt":"2021-05-07T08:39:15","slug":"testing-properties","status":"publish","type":"post","link":"https:\/\/www.antunkarlovac.com\/blog\/2008\/06\/24\/testing-properties\/","title":{"rendered":"Testing Properties"},"content":{"rendered":"<p>In LZX, it&#8217;s often sensible to test JavaScript properties before using them, to be sure that they are defined, e.g.<\/p>\n<p><code>\/\/ Bad example<br \/>\nif (someObject.someValue) {<br \/>\nsetAttribute(\"x\", someObject.someValue);<br \/>\n} else {<br \/>\nvar defaultValue = 123;<br \/>\nsetAttribute(\"x\", defaultValue);<br \/>\n}<\/code><\/p>\n<p>In the above example, if someObject.someValue were <em>undefined<\/em>, then the conditional test would fail and the default value would be used.\u00a0 Here\u00a0<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;rothwelldouglas&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:268476,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;10&quot;:2,&quot;14&quot;:{&quot;1&quot;:2,&quot;2&quot;:3700253},&quot;15&quot;:&quot;Arial&quot;,&quot;21&quot;:1}\" data-sheets-hyperlink=\"http:\/\/rothwelldouglas.com\/\" data-sheets-hyperlinkruns=\"[null,0,&quot;http:\/\/rothwelldouglas.com\/&quot;]\"><a class=\"in-cell-link\" href=\"http:\/\/rothwelldouglas.com\/\" target=\"_blank\" rel=\"noopener\">rothwelldouglas<\/a><\/span> you can check other basic examples of testing properties. However there are a couple of problems in the above code. For more examples must check <span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;songsforromance&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:268476,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;10&quot;:2,&quot;14&quot;:{&quot;1&quot;:2,&quot;2&quot;:3700253},&quot;15&quot;:&quot;Arial&quot;,&quot;21&quot;:1}\" data-sheets-hyperlink=\"http:\/\/songsforromance.com\/\" data-sheets-hyperlinkruns=\"[null,0,&quot;http:\/\/songsforromance.com\/&quot;]\"><a class=\"in-cell-link\" href=\"http:\/\/songsforromance.com\/\" target=\"_blank\" rel=\"noopener\">songsforromance<\/a> .<\/span><\/p>\n<p>Firstly, if someValue is undefined, referencing it with a dot syntax (i.e. someObject.someValue) will throw a Debugger warning that reads &#8220;reference to undefined property &#8220;someValue&#8221;). You don&#8217;t want to have lots of debugger warnings in your application. Your application will run more slowly as the Debugger generates the warnings (only with the debugger open, though), and you might miss important warnings in the noise. To get more information visit\u00a0<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;elizabethnelsonstudio.com&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:268476,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;10&quot;:2,&quot;14&quot;:[null,2,3700253],&quot;15&quot;:&quot;Arial&quot;,&quot;21&quot;:1}\" data-sheets-hyperlink=\"http:\/\/elizabethnelsonstudio.com\" data-sheets-hyperlinkruns=\"[null,0,&quot;http:\/\/elizabethnelsonstudio.com&quot;]\"><a class=\"in-cell-link\" href=\"http:\/\/elizabethnelsonstudio.com\" target=\"_blank\" rel=\"noopener\">elizabethnelsonstudio<\/a><\/span> <span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;wrice&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:268476,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;10&quot;:2,&quot;14&quot;:{&quot;1&quot;:2,&quot;2&quot;:3700253},&quot;15&quot;:&quot;Arial&quot;,&quot;21&quot;:1}\" data-sheets-hyperlink=\"http:\/\/wrice.com.au\/\" data-sheets-hyperlinkruns=\"{&quot;1&quot;:0,&quot;2&quot;:&quot;http:\/\/wrice.com.au\/&quot;}\">.<\/span><\/p>\n<p>Secondly, if someValue is actually defined as something that will equate to <em>false <\/em>in a conditional, then the conditional will fail. In the above example, if someValue was the number 0 (zero), the conditional test would fail, since in the Flash runtime the number 0 converts to a boolean false. (See this handy <a href=\"http:\/\/choronzon.net\/Jared\/OReilly%20Bookshelves\/web2\/action\/ch03_04.htm\" target=\"_blank\" rel=\"noopener\">O&#8217;Reilly article<\/a> on Data Type Conversion in Flash).<\/p>\n<p>A better way to test the above variable before using it would be:<br \/>\n<code><br \/>\nif (someObject[\"someValue\"] != undefined) {<br \/>\nsetAttribute(\"x\", someObject.someValue);<br \/>\n} else {<br \/>\nvar defaultValue = 123;<br \/>\nsetAttribute(\"x\", defaultValue);<br \/>\n}\u00a0<\/code><\/p>\n<p>For more examples visit at <a href=\"https:\/\/topquartile.org\">topquartile<\/a> .<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In LZX, it&#8217;s often sensible to test JavaScript properties before using them, to be sure that they are defined, e.g. \/\/ Bad example if (someObject.someValue) { setAttribute(&#8220;x&#8221;, someObject.someValue); } else { var defaultValue = 123; setAttribute(&#8220;x&#8221;, defaultValue); } In the above example, if someObject.someValue were undefined, then the conditional test would fail and the default &hellip; <a href=\"https:\/\/www.antunkarlovac.com\/blog\/2008\/06\/24\/testing-properties\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Testing Properties<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":5,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":410,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions\/410"}],"wp:attachment":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}