{"id":226,"date":"2010-02-24T13:31:09","date_gmt":"2010-02-24T18:31:09","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=226"},"modified":"2010-02-24T13:31:09","modified_gmt":"2010-02-24T18:31:09","slug":"namespace-encapsulation-in-javascript","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/02\/24\/namespace-encapsulation-in-javascript\/","title":{"rendered":"Namespace, Encapsulation in JavaScript"},"content":{"rendered":"<p>I&#8217;ve had several discussion recently some of the more advanced features of JavaScript, such as functions as return values, namespaces, encapsulation, etc.  In order to demonstrate some of these things, I contrived a simple example of a dependency injection tool. <\/p>\n<p>Never mind that dependency injection is not really a relevant pattern in JavaScript or other dynamic languages.  It just fit all the pieces I wanted to demo.  The Rhino JavaScript engine was used to run and test the example.<\/p>\n<p>For more on how this stuff works, check out Douglas Crockford&#8217;s <a href=\"http:\/\/www.amazon.com\/gp\/product\/0596517742?ie=UTF8&#038;tag=timhoosblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0596517742\">JavaScript: The Good Parts<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=timhoosblo-20&#038;l=as2&#038;o=1&#038;a=0596517742\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><\/p>\n<pre>\r\n<code>\/\/ObjectMap namespace\r\nvar ObjectMap = function (){\r\n    var that = this;\r\n\r\n    that.container = function () {\r\n        var registry = {};\r\n        var fact = {};\r\n\r\n        fact.register = function (name, constructor) {\r\n            registry[name] = constructor;\r\n        };\r\n\r\n        fact.getObject = function (theType) { \r\n            return registry[theType]();\r\n        };\r\n        return fact;\r\n    }();\r\n   \r\n    return that;\r\n}();\r\n\r\n\/\/Dog constructor, with encapsulated private name\r\nvar Dog = function () {\r\n    var d = {};\r\n    var name = \"spot\";\r\n    d.getName = function () {return name;};\r\n    d.setName = function (val) {name=val; return d;};\r\n    d.speak = function () {return d.getName() + \" barks\";};\r\n    return d;\r\n};\r\n\r\n\/\/register Dog with the DI tool\r\nObjectMap.container.register(\"Dog\", Dog);\r\n\r\n\/\/get and use an object function the DI tool \r\nvar munson = ObjectMap.container.getObject(\"Dog\");\r\nmunson.setName(\"Munson\");\r\nprint(munson.speak());<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had several discussion recently some of the more advanced features of JavaScript, such as functions as return values, namespaces, encapsulation, etc. In order to demonstrate some of these things, I contrived a simple example of a dependency injection tool. Never mind that dependency injection is not really a relevant pattern in JavaScript or other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54,18],"tags":[276,69,60,272],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","tag-javascript","tag-linkedin","tag-objects","tag-programming"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}