{"id":942,"date":"2017-04-29T09:30:57","date_gmt":"2017-04-29T17:30:57","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=942"},"modified":"2017-04-29T09:31:23","modified_gmt":"2017-04-29T17:31:23","slug":"how-to-achieve-static-variable-in-javascript","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=942","title":{"rendered":"How to achieve static variable in javascript"},"content":{"rendered":"<p>2 Ways : <\/p>\n<p><strong>1) Make use of the fact that functions are objects and can have properties<\/strong><br \/>\n(Even using &#8220;this&#8221; instead of uniqueID inside the function will do)<\/p>\n<pre>\r\nfunction uniqueID() {\r\n    \/\/ Check to see if the counter has been initialized\r\n    if ( typeof uniqueID.counter == 'undefined' ) {\r\n        \/\/ It has not... perform the initialization\r\n        uniqueID.counter = 0;\r\n    }\r\n    uniqueID.counter++;\r\n    return (uniqueID.counter);\r\n}\r\n\r\nconsole.log(uniqueID());\r\nconsole.log(uniqueID());\r\n\r\n\r\n<\/pre>\n<p><strong>2) Using closure and function expressions<\/strong><\/p>\n<pre>\r\nvar uniqueID = (function() {\r\n   var id = 0; \/\/ This is the private persistent value\r\n   \/\/ The outer function returns a nested function that has access\r\n   \/\/ to the persistent value.  It is this nested function we're storing\r\n   \/\/ in the variable uniqueID above.\r\n   return function() { return id++; };  \/\/ Return and increment\r\n})(); \/\/ Invoke the outer function after defining it.\r\n\r\nconsole.log(uniqueID());\r\nconsole.log(uniqueID());\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2 Ways : 1) Make use of the fact that functions are objects and can have properties (Even using &#8220;this&#8221; instead of uniqueID inside the function will do) function uniqueID() { \/\/ Check to see if the counter has been initialized if ( typeof uniqueID.counter == &#8216;undefined&#8217; ) { \/\/ It has not&#8230; perform the &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=942\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to achieve static variable in javascript<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-942","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=942"}],"version-history":[{"count":2,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":944,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions\/944"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}