{"id":135,"date":"2009-07-17T11:59:09","date_gmt":"2009-07-17T06:29:09","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=135"},"modified":"2009-07-17T12:01:06","modified_gmt":"2009-07-17T06:31:06","slug":"static-variables-in-javascript","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/javascript\/static-variables-in-javascript\/","title":{"rendered":"static variables in javascript"},"content":{"rendered":"<p>static variables in javascript<\/p>\n<p>In javascript functions and objects work in the same manner. Functions are also objects.<br \/>\nso a function can have a member variable like in objects.<br \/>\nA member variable will retain its value between function calls.<br \/>\n So this aspect is used for having a static variable in javascript.<br \/>\nActually there is no option like static variables in javascript. so here the member variable server as a static variable in javascript.<\/p>\n<p>here is one simple script which uses a static variable to maintain a counter<\/p>\n<pre lang=\"javascript\">\r\n<script>\r\nfunction addmore()\r\n{\r\n\tif(!addmore.counter)addmore.counter = 0;\r\n\taddmore.counter++;\r\n\talert(addmore.counter)\r\n}\r\n\r\naddmore();\r\naddmore();\r\n\r\n<\/script>\r\n<\/pre>\n<p>or here is another version <\/p>\n<pre lang=\"javascript\">\r\n<script>\r\nfunction addmore()\r\n{\r\n\tif(!this.counter)this.counter = 0;\r\n\tthis.counter++;\r\n\talert(this.counter)\r\n}\r\n\r\naddmore();\r\naddmore();\r\n\r\n<\/script>\r\n<\/pre>\n<p>i have used this for multiple file uploads where there will be an addmore button and a variable to maintain the count of number of uploads to display and once this.counter is greater than 10 then i alert a message that uploads cannot be more than 10.<\/p>\n<p>some people had an idea that why done we use like the following instead of using it like a member function<\/p>\n<pre lang=\"javascript\">\r\n<script>\r\nvar counter=0\r\nfunction addmore()\r\n{\r\n\tcounter++;\r\n\talert(counter);\r\n}\r\naddmore();\r\naddmore();\r\n\r\n<\/script>\r\n\r\n<\/pre>\n<p>my answer is not to mess up the code with too many declarations which uses the word var&#8230;<br \/>\nafter a long time we would wonder which variable is for which function<br \/>\nand if the variable is with respect to the context of the process then that will a good practice of maintaing standards and more meaningful.<\/p>\n<p>if you would like to suggest an alternate please comment on this because i would like to know more if there is anything beyond my scope on this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>static variables in javascript In javascript functions and objects work in the same manner. Functions are also objects. so a function can have a member variable like in objects. A member variable will retain its value between function calls. So this aspect is used for having a static variable in javascript. Actually there is no [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":3,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":138,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/135\/revisions\/138"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}