{"id":624,"date":"2012-05-19T00:36:51","date_gmt":"2012-05-19T00:36:51","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=624"},"modified":"2012-05-19T00:37:03","modified_gmt":"2012-05-19T00:37:03","slug":"javascript-self-invoking-anonymous-functions","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/javascript-self-invoking-anonymous-functions\/","title":{"rendered":"JavaScript and self-invoking anonymous functions"},"content":{"rendered":"<p>That title is a mouthful, but I wanted to make sure to identify this syntax properly as we&#39;ve all seen them, but probably didn&#39;t understand what they were.<\/p>\n<p>You might notice these self-invoking functions in 3rd party functions or jQuery plugins, as they are generally used by more seasoned developers. Okay, so what do they look like?<\/p>\n<p><code>(<br \/>\n\t&nbsp; function(intro, name) {<br \/>\n\t&nbsp;&nbsp;&nbsp; var output = intro + &#39;, &#39; + name;<br \/>\n\t&nbsp;&nbsp;&nbsp; document.write(output);<br \/>\n\t&nbsp; }<br \/>\n\t) (&#39;Hello&#39;, &#39;Bob&#39;)<\/code><\/p>\n<p>\n\tLooks kinda like we wasted 4 lines of code! But, if you&#39;re familiar with variable scope in JavaScript, you&#39;ll understand that what we actually did is avoid creating a global variable. Specifically, we did not declare the output variable in a global scope.<\/p>\n<p>Any variable created inside a function is owned by that function, and hence destroyed (set to undefined) once execution of the function is complete.<\/p>\n<p>So, although we have a few extra lines of code, it may be worth it, as it makes your code a little less complex by reducing the number of global variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>That title is a mouthful, but I wanted to make sure to identify this syntax properly as we&#39;ve all seen them, but probably didn&#39;t understand&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[178],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/624"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":2,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":626,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/624\/revisions\/626"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}