{"id":456,"date":"2013-06-12T07:36:09","date_gmt":"2013-06-12T07:36:09","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=456"},"modified":"2013-06-12T07:36:51","modified_gmt":"2013-06-12T07:36:51","slug":"javascript-refresher-properties-methods-and-types","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/javascript-refresher-properties-methods-and-types\/","title":{"rendered":"JavaScript Refresher &#8211; Properties, Methods and Types"},"content":{"rendered":"<p><strong>Properties and Methods<\/strong><\/p>\n<p>JavaScript is a dynamic language. We do not need to declare properties or methods on an object prior to using them, for example the following will automatically add a new property and method to an object<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar o = new Object();\r\no.Message = &quot;Clicked&quot;;\r\no.Clicked = function() {\r\n   alert(this.Message);\r\n}\r\n<\/pre>\n<p>So in the above code we create an instance of an Object and then by calling o.Message we basically add a new property to the object o. Next we create a method on the object. Note: We need to use &#8220;this&#8221; to access the property from within the method otherwise the code tries to access an undefined variable.<\/p>\n<p>We can actually add properties and methods as well as access them using indexer notation, for example<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar o = new Object();\r\no&#x5B;&quot;Message&quot;] = &quot;Hello&quot;;\r\n<\/pre>\n<p>and this allows us to even call methods dynamically such as<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar clicked = &quot;Clicked&quot;;\r\n\r\nvar o = new Object();\r\n   o&#x5B;&quot;Message&quot;] = &quot;Clicked&quot;;\r\n   o&#x5B;&quot;Clicked&quot;] = function() {\r\n      alert(this&#x5B;&quot;Message&quot;]);\r\n   }\r\n\r\n\r\n   o&#x5B;clicked]();\r\n<\/pre>\n<p>So in the above we can dynamically change the method called (assuming we had more than one method).<\/p>\n<p>We can also iterate over the members of an object using the following <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar o = new Object();\r\n   o&#x5B;&quot;Message&quot;] = &quot;Clicked&quot;;\r\n   o&#x5B;&quot;Clicked&quot;] = function() {\r\n      var s = &quot;&quot;;\r\n      \/\/ iterate over the object\r\n      for(var p in o)\r\n      {\r\n         s = s + p + &quot;;&quot;\r\n      }\r\n      alert(s);\r\n   }\r\n<\/pre>\n<p>Using the for..in loop we simply iterate over and object, the string &#8220;s&#8221; will be &#8220;Message;Clicked&#8221;.<\/p>\n<p>Finally, we can add properties and methods, but we can also delete them using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndelete o.Message;\r\n<\/pre>\n<p>This deletes the Message property from the object o.<\/p>\n<p><strong>Types<\/strong><\/p>\n<p>JavaScript is loosely types. We declare variables using the var keyword and a variable can change type at any time, for example<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar tmp = 3.1415926;\r\ntmp = &quot;Hello&quot;;\r\n<\/pre>\n<p>The runtime will not complain about the type conversion from a floating point number to a string.<\/p>\n<p>JavaScript has a small number of types which include, string, number, boolean, array and object.<\/p>\n<ul>\n<li>A string may be declared using either double quotes or single quotes, obviously this helps when the string is declare inline.<\/li>\n<li>A number can be declared with or without decimal places (in other words it can be floating point or an integer).<\/li>\n<li>A boolean may be true or false (as one would expect)<\/li>\n<li>An array can be declared in various ways (described later) and can store heterogeneous data types<\/li>\n<li>An object can be declared and properties and methods assigned to it (see above)<\/li>\n<\/ul>\n<p>If a var is not assigned a value it is &#8220;undefined&#8221; (which is itself a type), basically this is a variable that has no value, but we can also use null to denote an &#8220;empty&#8221; value.<\/p>\n<p><em>Declaring Arrays<\/em><\/p>\n<p>Arrays can be declared in various ways, for the first example we declare an array and then we dynamically add items by creating an index to them<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar vowels = new Array();\r\nvowels&#x5B;0] = &quot;a&quot;;\r\nvowels&#x5B;1] = &quot;e&quot;;\r\nvowels&#x5B;2] = &quot;i&quot;;\r\nvowels&#x5B;3] = &quot;o&quot;;\r\nvowels&#x5B;4] = &quot;u&quot;;\r\n<\/pre>\n<p>A couple of alternatives are <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar vowels1 = new Array(&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;);\r\nvar vowels2 = &#x5B;&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Properties and Methods JavaScript is a dynamic language. We do not need to declare properties or methods on an object prior to using them, for example the following will automatically add a new property and method to an object var o = new Object(); o.Message = &quot;Clicked&quot;; o.Clicked = function() { alert(this.Message); } So in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":463,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions\/463"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}