<?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>iExpert Ltd &#187; global variable conflicts</title>
	<atom:link href="http://www.iexpert.biz/archives/tag/global-variable-conflicts/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iexpert.biz</link>
	<description></description>
	<lastBuildDate>Tue, 25 May 2010 02:41:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JQuery, mootools and other javascript global variable conflicts</title>
		<link>http://www.iexpert.biz/archives/295?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=jquery-mootools-and-other-javascript-global-variable-conflicts</link>
		<comments>http://www.iexpert.biz/archives/295#comments</comments>
		<pubDate>Sun, 07 Mar 2010 07:19:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[global variable conflicts]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://www.iexpert.biz/?p=295</guid>
		<description><![CDATA[This is how easy it is to avoid javascript conflicts with mootools or prototype or whatever.  Just put
all your logic into a private scoped function &#8230; it can have its own variables, other functions, event handlers, etc.
It can talk to all the same DOM elements any other javascript library can. 

 Using the $ Alias [...]]]></description>
			<content:encoded><![CDATA[<div>This is how easy it is to avoid javascript conflicts with mootools or prototype or whatever.  Just put</div>
<div>all your logic into a private scoped function &#8230; it can have its own variables, other functions, event handlers, etc.</div>
<div>It can talk to all the same DOM elements any other javascript library can.<strong> </strong></div>
<div></div>
<div><strong> Using the $ Alias Without Creating Global Conflicts</strong></div>
<p><strong><em>Problem</em></strong></p>
<p>You want to use the shortcut $ alias instead of typing the global namespace name<br />
(jQuery) without fear of global conflicts.</p>
<p><em><strong>Solution</strong></em></p>
<p>The solution here is to create an anonymous self-invoking function that we pass the<br />
jQuery object to and then use the $ character as a parameter pointer to the jQuery<br />
object.</p>
<p>For example, all jQuery code could be encapsulated inside the following self-invoking<br />
function:</p>
<p>(function($)  { //function to create private scope with $ parameter<br />
//private scope and using $ without worry of conflict<br />
$.someJQueryFunction();<br />
$(&#8216;td&#8217;).doSomethingToAllTDElements(function() {<br />
});<br />
//<br />
})(jQuery);     //invoke nameless function and pass it the jQuery object</p>
<p>It really looks like this:</p>
<p>(function($) { } )(jQuery);</p>
<p>an example of something similar:</p>
<p>(function(someVar) {<br />
alert(&#8220;someVar = &#8221; + someVar);<br />
})(&#8220;blah&#8221;);</p>
<p>would trigger an alert saying:  someVar = blah</p>
<p>Discussion</p>
<p>Essentially, what is going on here is that we have passed the global reference to jQuery<br />
to a function that creates a private scope. Had we not done this and chosen to use the<br />
shorthand $ alias in the global scope, we would be taking a risk by assuming that no<br />
other scripts included in the HTML document (or scripts included in the future) use<br />
the $ character.</p>
<p>Why risk it when you can just create your own private scope?</p>
<p>Another advantage to doing this is that code included inside of the anonymous selfinvoking<br />
function will run in its own private scope. You can rest assured that anything<br />
that is placed inside the function will likely never cause a conflict with other JavaScript<br />
code written in the global scope. So, again, why risk programmatic collisions? Just<br />
create your own private scope.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iexpert.biz/archives/295/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
