{"id":11744,"date":"2014-02-14T13:53:59","date_gmt":"2014-02-14T08:23:59","guid":{"rendered":"https:\/\/2thenew.xyz\/blog\/?p=11744"},"modified":"2016-12-19T15:31:06","modified_gmt":"2016-12-19T10:01:06","slug":"singleton-pattern-with-javascript","status":"publish","type":"post","link":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/","title":{"rendered":"Singleton Pattern with JavaScript"},"content":{"rendered":"<p>Many a times, we face a requirement where we need only one object of a class to communicate across the application. <strong>Singleton<\/strong> <strong>Pattern<\/strong> comes into role in this scenario. <strong>Singleton<\/strong> is one of my favorite <strong>Design<\/strong> <strong>Patterns<\/strong>. <strong>Singleton<\/strong> <strong>pattern<\/strong> restricts us to initialize more than one object. We implement <strong>Singleton<\/strong> by creating a class, having a method named as <strong>getInstance()<\/strong>, which returns reference to object of that class. If object has already been initiated, then it will return reference to that object, otherwise will initiate a new instance and return reference to that newly initiated object. In <strong>JavaScript<\/strong>, we can easily implement <strong>Singleton<\/strong> <strong>Pattern<\/strong>.<\/p>\n<p>[js]<br \/>\n\/*<br \/>\n * ApplicationContext is Singleton class.<br \/>\n *\/<br \/>\nvar ApplicationContext = (function () {<br \/>\n    var instance; \/\/ instance variable to hold the reference of Object.<br \/>\n    function initialize() {<br \/>\n        \/\/ private members here<br \/>\n        var _this = this;<br \/>\n        var secretKey = +new Date() + &quot;&quot; + parseInt(Math.random()*1000, 10);<br \/>\n        return {<br \/>\n            \/\/ public members.<br \/>\n            getSecretKey: function () {<br \/>\n                return port;<br \/>\n            }<br \/>\n        };<br \/>\n    }<br \/>\n    return {<br \/>\n        \/*<br \/>\n         * If instance variable is already having reference to the ApplicationContext, then return it,<br \/>\n         * Otherwise initialize the instance variable first and then return.<br \/>\n         *\/<br \/>\n        getInstance: function () {<br \/>\n            if (!instance) {<br \/>\n                instance = initialize();<br \/>\n            }<br \/>\n            return instance;<br \/>\n        }<br \/>\n    };<br \/>\n})();<br \/>\nvar app1 = ApplicationContext.getInstance();<br \/>\nvar app2 = ApplicationContext.getInstance();<br \/>\nconsole.log(app1.getSecretKey() == app2.getSecretKey()); \/\/ true<br \/>\n[\/js]<\/p>\n<p><strong>app1<\/strong> and <strong>app2<\/strong> both are having same object, so <strong>app1.getSecretKey() == app2.getSecretKey()<\/strong> is logging true.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"https:\/\/2thenew.xyz\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[1185],"tags":[55,1308,1177,1309],"class_list":["post-11744","post","type-post","status-publish","format-standard","hentry","category-node-js-2","tag-javascript","tag-js","tag-nodejs-2","tag-singleton"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Amit Kumar\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Singleton Pattern with JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Singleton Pattern with JavaScript | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#article\",\"name\":\"Singleton Pattern with JavaScript | TO THE NEW Blog\",\"headline\":\"Singleton Pattern with JavaScript\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-02-14T13:53:59+05:30\",\"dateModified\":\"2016-12-19T15:31:06+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#webpage\"},\"articleSection\":\"Node.js, javascript, js, nodejs, singleton\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#listItem\",\"name\":\"Singleton Pattern with JavaScript\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#listItem\",\"position\":3,\"name\":\"Singleton Pattern with JavaScript\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/859356966acd3c3879759369c7ac72cfb81228287bfc78e99c3080bee9b6b9ba?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Amit Kumar\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/\",\"name\":\"Singleton Pattern with JavaScript | TO THE NEW Blog\",\"description\":\"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/singleton-pattern-with-javascript\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit-kumar\\\/#author\"},\"datePublished\":\"2014-02-14T13:53:59+05:30\",\"dateModified\":\"2016-12-19T15:31:06+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Singleton Pattern with JavaScript | TO THE NEW Blog","description":"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having","canonical_url":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#article","name":"Singleton Pattern with JavaScript | TO THE NEW Blog","headline":"Singleton Pattern with JavaScript","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2014-02-14T13:53:59+05:30","dateModified":"2016-12-19T15:31:06+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#webpage"},"articleSection":"Node.js, javascript, js, nodejs, singleton"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#listItem","name":"Singleton Pattern with JavaScript"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#listItem","position":3,"name":"Singleton Pattern with JavaScript","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/","name":"Amit Kumar","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/859356966acd3c3879759369c7ac72cfb81228287bfc78e99c3080bee9b6b9ba?s=96&d=mm&r=g","width":96,"height":96,"caption":"Amit Kumar"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/","name":"Singleton Pattern with JavaScript | TO THE NEW Blog","description":"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/amit-kumar\/#author"},"datePublished":"2014-02-14T13:53:59+05:30","dateModified":"2016-12-19T15:31:06+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Singleton Pattern with JavaScript | TO THE NEW Blog","og:description":"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having","og:url":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Singleton Pattern with JavaScript | TO THE NEW Blog","twitter:description":"Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"11744","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:05:11","updated":"2024-02-29 10:49:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tSingleton Pattern with JavaScript\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Node.js","link":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/"},{"label":"Singleton Pattern with JavaScript","link":"https:\/\/2thenew.xyz\/blog\/singleton-pattern-with-javascript\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/11744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=11744"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/11744\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=11744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=11744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=11744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}