{"id":294,"date":"2013-04-30T21:50:26","date_gmt":"2013-04-30T21:50:26","guid":{"rendered":"http:\/\/www.webninjataylor.com\/library\/?p=294"},"modified":"2014-06-24T21:25:40","modified_gmt":"2014-06-24T21:25:40","slug":"javascript-pattern-revealing-module","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/javascript-pattern-revealing-module\/","title":{"rendered":"JavaScript Pattern: Revealing Module"},"content":{"rendered":"<p>This pattern is essentially the same as the Module Pattern, except all of the functions and variables are defined in the private scope, and we return an object with pointers to the private functionality that we wish to make public.<\/p>\n<pre>var myRevealingModule = function () {\r\n\r\n        var privateCounter = 0;\r\n\r\n        function privateFunction() {\r\n            privateCounter++;\r\n        }\r\n\r\n        function publicFunction() {\r\n            publicIncrement();\r\n        }\r\n\r\n        function publicIncrement() {\r\n            privateFunction();\r\n        }\r\n\r\n        function publicGetCount(){\r\n          return privateCounter;\r\n        }\r\n\r\n        \/\/ Reveal public pointers to \r\n        \/\/ private functions and properties        \r\n\r\n       return {\r\n            start: publicFunction,\r\n            increment: publicIncrement,\r\n            count: publicGetCount\r\n        };\r\n\r\n    }();\r\n<\/pre>\n<p>The main advantage to this approach is organization and readability. With this approach, we can also define a more specific naming scheme for the module for public methods.<\/p>\n<pre>myRevealingModule.start();<\/pre>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/www.uifuel.com\/learning-javascript-design-patterns\/?utm_source=Fresh+Brewed+Frontend&amp;utm_campaign=fcb2d6768b-Fresh_Brewed_Frontend_Newsletter&amp;utm_medium=email\">Learning JavaScript Design Patterns, Mike King (3\/22\/2013)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This pattern is essentially the same as the Module Pattern, except all of the functions and variables are defined in the private scope, and we return an object with pointers to the private functionality that we wish to make public. var myRevealingModule = function () { var privateCounter = 0; function privateFunction() { privateCounter++; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[9],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-javascript"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":5,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":3372,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/294\/revisions\/3372"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}