<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>closures &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/closures/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Writing a jQuery plugin &#8211; (part 1). Good practices!</title>
		<link>/2009/11/13/writing-a-jquery-plugin-part-1-good-practices/</link>
		<comments>/2009/11/13/writing-a-jquery-plugin-part-1-good-practices/#respond</comments>
		<pubDate>Fri, 13 Nov 2009 07:37:02 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[flexibility]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[legacy]]></category>

		<guid isPermaLink="false">/?p=790</guid>
		<description><![CDATA[JavaScript flexibility In JS you have the natural flexibility to extend an object you have previously declared. Even more you can extend any native objects! That makes the writing of a jQuery, or whatever javascript library, plugins to be very easy. As you may know in JS you can have object declared like that: var &#8230; <a href="/2009/11/13/writing-a-jquery-plugin-part-1-good-practices/" class="more-link">Continue reading <span class="screen-reader-text">Writing a jQuery plugin &#8211; (part 1). Good practices!</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/01/writing-a-jquery-plugin-part-2-sample-plugin/" rel="bookmark" title="Writing a jQuery plugin &#8211; (part 2). Sample plugin.">Writing a jQuery plugin &#8211; (part 2). Sample plugin. </a></li>
<li><a href="/2009/11/10/jquery-css-functions-part-1-offset/" rel="bookmark" title="jQuery CSS functions. Part 1 &#8211; offset()">jQuery CSS functions. Part 1 &#8211; offset() </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/17/clickoutside-jquery-plugin/" rel="bookmark" title="clickoutside jQuery plugin">clickoutside jQuery plugin </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>JavaScript flexibility</h2>
<p>In JS you have the natural flexibility to extend an object you have previously declared. Even more you can extend any native objects! That makes the writing of a <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a>, or whatever javascript library, plugins to be very easy.</p>
<p>As you may know in JS you can have object declared like that:</p>
<blockquote>
<pre>var obj = { a : 1, b : 2 };</pre>
</blockquote>
<p>That is an object with two properties <strong>a</strong> and <strong>b</strong> which values are respectively <strong>1</strong> and <strong>2</strong>. If you&#8217;d like to extend this object later you can just write:</p>
<blockquote>
<pre>obj.c = 3</pre>
</blockquote>
<p>and now the object looks like that:</p>
<blockquote>
<pre>{ a : 1, b : 2, c : 3 }</pre>
</blockquote>
<p>That&#8217;s perfect when you&#8217;d like to write plugins to jQuery.<span id="more-790"></span></p>
<p>As you may know the $ sign in jQuery is synonym of the function called jQuery, so you can add any property to that library with just writing:</p>
<blockquote>
<pre>jQuery.myMethod = function() {}</pre>
</blockquote>
<h2>JS closures</h2>
<p>What are closures? Well you know in almost every programming language you&#8217;ve scope of the variables. In JavaScript instead if you declare a variable on the global level:</p>
<blockquote>
<pre>var a = 1;</pre>
</blockquote>
<p>you can access this variable right from everywhere! That&#8217;s not good at all. If you later declare a function and there you initialize a variable with the same name &#8211; the scope of the variables make visible and useful only the inner variable declared within the function.</p>
<p>This helps the programmers to force the usage of pseudo private properties of an object.</p>
<p>The technique is quite simple. You make a anonymous function:</p>
<blockquote>
<pre>(function() {})();</pre>
</blockquote>
<p>and every variable inside is not visible outside. Thus you can export only specific features using both the window object or parameters:</p>
<blockquote>
<pre>var externalObject = {};</pre>
</blockquote>
<blockquote>
<pre>(function(o) {
   var myObject = { a : 1, b : 2}
   o.a = myObject.a;
})(externalObject);</pre>
</blockquote>
<p>Thus you export only one of the properties of myObject. Instead without the closure both a and b properties would be visible to the world.</p>
<p>In help comes also the fact that this anonymous functions are &#8220;self-called&#8221; so you don&#8217;t have to call them after the declaration.</p>
<h2>Legacy code and good practices</h2>
<p>To come back on the jQuery plugins &#8211; it&#8217;s good practice to make your plugins in closures so they should not interfere with other code written before.</p>
<p>And one more time &#8211; use the jQuery object for your plugins instead of the $ sign, because some libraries different from jQuery use this sign globally and you can have problems with that.</p>
<p>In my next post I&#8217;ll write a simple plugin so you could see how it can be done with very small code chunk.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/01/writing-a-jquery-plugin-part-2-sample-plugin/" rel="bookmark" title="Writing a jQuery plugin &#8211; (part 2). Sample plugin.">Writing a jQuery plugin &#8211; (part 2). Sample plugin. </a></li>
<li><a href="/2009/11/10/jquery-css-functions-part-1-offset/" rel="bookmark" title="jQuery CSS functions. Part 1 &#8211; offset()">jQuery CSS functions. Part 1 &#8211; offset() </a></li>
<li><a href="/2009/07/27/jquery-debug-plugin/" rel="bookmark" title="jQuery debug plugin">jQuery debug plugin </a></li>
<li><a href="/2010/02/17/clickoutside-jquery-plugin/" rel="bookmark" title="clickoutside jQuery plugin">clickoutside jQuery plugin </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2009/11/13/writing-a-jquery-plugin-part-1-good-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript closures in brief</title>
		<link>/2009/07/08/javascript-closures-in-brief/</link>
		<comments>/2009/07/08/javascript-closures-in-brief/#respond</comments>
		<pubDate>Wed, 08 Jul 2009 06:09:45 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">/?p=662</guid>
		<description><![CDATA[What&#8217;s a closure Well every javascript programer knows that a variable defined in javascript file with the var declaration is global into all javascript code. var a = 12; console.log(a); that code prints 12 into the console. Note: console object is visible in Firefox and Safari, but not in IE, and here&#8217;s used just for &#8230; <a href="/2009/07/08/javascript-closures-in-brief/" class="more-link">Continue reading <span class="screen-reader-text">JavaScript closures in brief</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/03/09/what-make-javascript-closures-work/" rel="bookmark" title="What make JavaScript closures work?">What make JavaScript closures work? </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/02/01/writing-a-jquery-plugin-part-2-sample-plugin/" rel="bookmark" title="Writing a jQuery plugin &#8211; (part 2). Sample plugin.">Writing a jQuery plugin &#8211; (part 2). Sample plugin. </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>What&#8217;s a closure</h2>
<p>Well every javascript programer knows that a variable defined in javascript file with the var declaration is global into all javascript code.</p>
<blockquote>
<pre>var a = 12;
console.log(a);</pre>
</blockquote>
<p>that code prints 12 into the console.</p>
<p><em><strong>Note: </strong>console object is visible in Firefox and Safari, but not in IE, and here&#8217;s used just for the test!</em></p>
<h2>Here comes the closures</h2>
<p>Let&#8217;s assume I&#8217;ve two javascript files.</p>
<p><strong>file1.js:</strong></p>
<blockquote>
<pre>var a = 12;</pre>
</blockquote>
<p><strong>file2.js:</strong></p>
<blockquote>
<pre>a = 13;</pre>
</blockquote>
<p>Than obviously if I print out the variable a in file2.js after that line, I&#8217;ll have the value of a equal to 13, and this will be the value of the variable a from the file1.js, because that&#8217;s obviously the same variable.<span id="more-662"></span></p>
<p>Now let&#8217;s make it &#8220;private&#8221; using closures. First define an anonymous function in file1.js</p>
<p><strong>file1.js:</strong></p>
<blockquote>
<pre>(function() {
    var a = 12;
})();</pre>
</blockquote>
<p>With that you define an anonymous function and the ending () you&#8217;re invoking that function. Now if you change the above code like that:</p>
<p><strong>file1.js:</strong></p>
<blockquote>
<pre>(function() {
    var a = 12;
    console.log(a);
})();</pre>
</blockquote>
<p>the javascript console in FF will print 12, but if you have the code in file2.js another variable with the same name that will be another variable. Let&#8217;s merge the two files for better example.</p>
<p><strong>file.js:</strong></p>
<blockquote>
<pre>var a = 13;

(function() {
    a = 12;
    console.log(a);
})();

console.log(a);</pre>
</blockquote>
<p>that code will print first 12 and than 13, and that makes clear what the closure function does. Without the closure it will look like this:</p>
<p><strong>file.js:</strong></p>
<blockquote>
<pre>var a = 13;

var f = function() {
    a = 12;
    console.log(a);
}
f();

console.log(a);</pre>
</blockquote>
<p>and that will print twice the value of a which is 12.</p>
<h2>How to export variables, functions and objects from a closure</h2>
<p>Simply by assigning the variables, objects, etc. to appropriate variables in the window object, like that:</p>
<p><strong>file.js:</strong></p>
<blockquote>
<pre>var a = 13;

(function() {
    a = 12;
    console.log(a);
    window.b = a;
})();

console.log(a);
console.log(b);</pre>
</blockquote>
<p>now you&#8217;ll have first printed 12 from the closure function, than 13 which is the value of the global variable a, and than again 12 which is the variable b, which equals to the closure a, and it&#8217;s exported with the help of the window object.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/03/09/what-make-javascript-closures-work/" rel="bookmark" title="What make JavaScript closures work?">What make JavaScript closures work? </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/02/01/writing-a-jquery-plugin-part-2-sample-plugin/" rel="bookmark" title="Writing a jQuery plugin &#8211; (part 2). Sample plugin.">Writing a jQuery plugin &#8211; (part 2). Sample plugin. </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2009/07/08/javascript-closures-in-brief/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
