<?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>legacy &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/legacy/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>
	</channel>
</rss>
