{"id":208,"date":"2014-06-20T03:42:21","date_gmt":"2014-06-20T11:42:21","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=208"},"modified":"2019-05-20T12:04:59","modified_gmt":"2019-05-20T20:04:59","slug":"closure-in-javascript","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=208","title":{"rendered":"Closure and Lexical Scoping in Javascript"},"content":{"rendered":"<div id=\"wiki-content\" class=\"column-main wiki-column text-content\">\n<article id=\"wikiArticle\">\n<p class=\"summary\">A <em>closure<\/em> is the combination of a function and the lexical environment within which that function was declared. Note that Merely accessing a variable outside of the immediate scope (no return statement is necessary) will create something called a closure. A function accessing a global variable will also create a closure as can be seen by console.dir(function).<\/p>\n<\/article>\n<\/div>\n<p>A closure is a function having access to the parent scope, even after the parent function has closed.<\/p>\n<p>One example of A closure is a function that returns a function. The function that is returned (the inner function) is created inside the called function (the outer) so &#8211; due to the scoping rules we\u2019ve seen &#8211; the inner has access to the variables and arguments of the outer.<\/p>\n<p>JavaScript variables can belong to the local or global scope. Global variables can be made local (private) with closures.<\/p>\n<p>Mozilla Development Network(MDN) gives a great definition:<br \/>\n\u201cA closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.\u201d<\/p>\n<p>JavaScript has lexical scoping with function scope. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function&#8217;s variables, and it has access to the global variables.<\/p>\n<p><a href=\"http:\/\/www.htmldog.com\/guides\/javascript\/advanced\/closures\/\">http:\/\/www.htmldog.com\/guides\/javascript\/advanced\/closures\/<\/a><\/p>\n<p><a href=\"http:\/\/howtonode.org\/why-use-closure\">http:\/\/howtonode.org\/why-use-closure<\/a><\/p>\n<p><a href=\"https:\/\/spin.atomicobject.com\/2014\/10\/20\/javascript-scope-closures\/\">https:\/\/spin.atomicobject.com\/2014\/10\/20\/javascript-scope-closures\/<\/a><\/p>\n<p>Uses:<br \/>\n1) Most useful for events and callbacks<br \/>\n2) Used to simulate private variables i.e, encapsulation. Read more on Private members in javascript and privileged methods here : <a href=\"http:\/\/www.crockford.com\/javascript\/private.html\">http:\/\/www.crockford.com\/javascript\/private.html<\/a><br \/>\n3) Used to create function factories<br \/>\n<a href=\"https:\/\/medium.com\/written-in-code\/practical-uses-for-closures-c65640ae7304\">https:\/\/medium.com\/written-in-code\/practical-uses-for-closures-c65640ae7304<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A closure is the combination of a function and the lexical environment within which that function was declared. Note that Merely accessing a variable outside of the immediate scope (no return statement is necessary) will create something called a closure. A function accessing a global variable will also create a closure as can be seen &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=208\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Closure and Lexical Scoping in Javascript<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-208","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\/208","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=208"}],"version-history":[{"count":12,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":1026,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/1026"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}