<?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>Boris Ding</title>
	<atom:link href="http://borisding.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://borisding.com</link>
	<description></description>
	<lastBuildDate>Sat, 27 Feb 2010 14:56:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: How to Write Your Own jQuery Plugins?</title>
		<link>http://borisding.com/2010/02/tutorial-how-to-write-your-own-jquery-plugins/</link>
		<comments>http://borisding.com/2010/02/tutorial-how-to-write-your-own-jquery-plugins/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 17:24:44 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1945</guid>
		<description><![CDATA[Not long ago, I had mentioned few things to note when authoring jQuery plugins. This post, I am going to put things in more practical way. Whereas, I will show you how to write our own jQuery plugin, step by step. In this tutorial, we are going to create a jQuery plugin that can be [...]


Related posts:<ol><li><a href='http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/' rel='bookmark' title='Permanent Link: Few Things to Note When Authoring jQuery Plugin'>Few Things to Note When Authoring jQuery Plugin</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li><li><a href='http://borisding.com/2009/06/faceboxfacebook-style-load-your-page-into-lightbox-on-the-fly/' rel='bookmark' title='Permanent Link: Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!'>Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Not long ago, I had mentioned <a href="http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/">few things to note</a> when authoring jQuery plugins. This post, I am going to put things in more practical way. Whereas, I will show you how to write our own jQuery plugin, step by step. In this tutorial, we are going to create a jQuery plugin that can be used to randomly change text colour. Before getting started, you can try out this <a href="http://borisding.com/demo/randcolour.php">demo</a> in action.</p>
<p>It is easy to author a jQuery plugin. I am using jQuery v1.3.2 in this tutorial and of course, you are free to use any other versions instead. For instance, the latest version, <a href="http://code.jquery.com/jquery-1.4.2.js">v1.4.2</a>.</p>
<p>Note: You are highly encouraged to use recent versions for the sake of stability and performance, as well as new features, etc. Personally, I recommend to use at least v1.3.2 and above.</p>
<p><b>Step 1: Include the jQuery core library file</b><br />
First and foremost, you are required to grab a jQuery core library and include it, typically within <font face='courier new'>&lt;header&gt;</font> tag. If you don&#8217;t know where to go, you can <a href="http://jquery.com/">get a copy of jQuery</a> core library from its official website. Here is how we include the file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>I have basically renamed the file as &#8216;jquery.js&#8217; which resides in the same web folder with my web page, which is supposed to be used in this tutorial.</p>
<p><b>Step 2: Scoping and Avoiding Conflict</b><br />
Once we included the core library, we can start writing our methods and bundle it as plugin. As I have already mentioned in my previous post, it&#8217;s always a good practice to scope the jQuery&#8217;s alias object, &#8216;$&#8217; and avoid any conflict with other libraries. So that, we can use the &#8216;$&#8217; object when authoring our own methods. To do so, write an anonymous function as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #006600; font-style: italic;">//put everything inside this function...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p><b>Step 3: Define a method</b><br />
Next, we are going to create a method that will be attached to jQuery&#8217;s <font face='courier new'>fn</font> object so that we can use our own method together with jQuery core methods, so-called &#8216;chaining&#8217;. Say, we defined a method named as <font face='courier new'>randColour</font> and attached to <font face='courier new'>$.fn</font> object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">randColour</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #006600; font-style: italic;">//our stuff are putting inside 'each' function...</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>To allow us to chain with jQuery&#8217;s core methods, we need to put our stuff in jQuery&#8217;s &#8216;each&#8217; function and return it so that it can be tied with others. Keep in mind that, the &#8216;<font face='courier new'>this</font>&#8216; reserved word here refers to jQuery object!</p>
<p><b>Step 4: Put things together</b><br />
Once we are done with the plugin &#8217;skeleton&#8217;, we can then really start writing our stuff and putting it inside &#8216;this.each()&#8217; function. This tutorial, we are going to define two functions: &#8216;<font face='courier new'>returnColour()</font>&#8216; and &#8216;<font face='courier new'>applyColour(obj)</font>&#8216;.</p>
<p>i)  <font face='courier new'>returnColour()</font> &#8211; This function will return a randomized colour in RGB format.<br />
ii) <font face='courier new'>applyColour(obj)</font> &#8211; As its name applies, we are passing HTML element object to this function and apply the randomized colour to selected text.</p>
<p>So, the complete code is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">randColour</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">//init applyColour</span>
       applyColour<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #006600; font-style: italic;">//apply colour to selected text</span>
        <span style="color: #003366; font-weight: bold;">function</span> applyColour<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> rColour <span style="color: #339933;">=</span> returnColour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;         
          $<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">:</span> rColour<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">//return a randomized colour</span>
        <span style="color: #003366; font-weight: bold;">function</span> returnColour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           <span style="color: #003366; font-weight: bold;">var</span> maxRgb <span style="color: #339933;">=</span> <span style="color: #CC0000;">256</span>;
		<span style="color: #003366; font-weight: bold;">var</span> rgbVal <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> rgbTxt <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;rgb(&quot;</span>;
		<span style="color: #006600; font-style: italic;">//iterate to get rgb value</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> 0; i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">3</span>; i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		 rgbVal<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> maxRgb <span style="color: #009900;">&#41;</span>;
		 <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		  rgbTxt <span style="color: #339933;">+=</span> rgbVal<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span>;
		 <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		   rgbTxt <span style="color: #339933;">+=</span>  rgbVal<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span>;
		 <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	     <span style="color: #000066; font-weight: bold;">return</span> rgbTxt;
       <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Perhaps, you have noticed that there was an additional line of code in &#8216;this.each()&#8217; function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">  applyColour<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>We basically use this line of code to invoke the function and passing an object to it once &#8216;randColour&#8217; is called. Please be mindful, the <font face='courier new'>this</font> word here is now referring to the current object of HTML element, and NOT jQuery object. To make it clearer, it can be read as &#8216;apply the colour to this selected HTML element&#8217;.</p>
<p><b>Step 5: Calling Our Defined Method</b><br />
OK, at this stage, we can call our own method through jQuery ready handler whenever we want! For example:</p>
<p>JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#hello&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">randColour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>HTML</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h1 id=&quot;hello&quot;&gt;Hello World!&lt;/h1&gt;
(To change text colour, please click browser's 'Refresh/Reload' button.)</pre></div></div>

<blockquote><p>
NOTE: Once you are done with the code, you can save it as separate JS file and name it as, &#8216;jquery.something.js&#8217;. For instance, you may name it as &#8216;jquery.randcolour.js&#8217; so that it can be included in any web page for reuse purpose.
</p></blockquote>


<p>Related posts:<ol><li><a href='http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/' rel='bookmark' title='Permanent Link: Few Things to Note When Authoring jQuery Plugin'>Few Things to Note When Authoring jQuery Plugin</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li><li><a href='http://borisding.com/2009/06/faceboxfacebook-style-load-your-page-into-lightbox-on-the-fly/' rel='bookmark' title='Permanent Link: Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!'>Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2010/02/tutorial-how-to-write-your-own-jquery-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nested Object Literal and Making Function as Object</title>
		<link>http://borisding.com/2010/02/nested-object-literal-and-making-function-as-object/</link>
		<comments>http://borisding.com/2010/02/nested-object-literal-and-making-function-as-object/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 04:17:21 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Object-literal]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1903</guid>
		<description><![CDATA[It is a simple task to define an Object Literal. For example:

 var myObject = &#123;
    property: &#34;Foo: Bar&#34;,
	oFnc: function&#40;&#41;&#123;
	 document.writeln&#40;&#34;Hello World!&#34;&#41;;	 
   &#125;	 
 &#125;;

As you can see, myObject is now playing as an object. To check on it, just run this line of code:

alert&#40;&#34;myObject is: &#34; + typeof [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>It is a simple task to define an Object Literal. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> <span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    property<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Foo: Bar&quot;</span><span style="color: #339933;">,</span>
	oFnc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	 document.<span style="color: #660066;">writeln</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>;	 
   <span style="color: #009900;">&#125;</span>	 
 <span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>As you can see, <font face='courier new'>myObject</font> is now playing as an object. To check on it, just run this line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myObject is: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">typeof</span> myObject<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>And, it will tell you <font face='courier new'>myObject</font> is an &#8216;object&#8217;, as follows:</p>
<div id="attachment_1902" class="wp-caption aligncenter" style="width: 375px"><a href="http://borisding.com/wp-content/uploads/2010/02/myobject.jpg"><img src="http://borisding.com/wp-content/uploads/2010/02/myobject.jpg" alt="" title="myobject" width="365" height="127" class="size-full wp-image-1902" /></a><p class="wp-caption-text">Object</p></div>
<p>So, to access its property/function, we can basically call it as simple as this:</p>
<p>To access property and display it in alert prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myObject.<span style="color: #660066;">property</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>To access function, we can do it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">myObject.<span style="color: #660066;">oFnc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>OK now, I wanted to add another group of Object Literal, say put it in <font face='courier new'>oFnc</font> function and then return it. So the complete code will look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    property<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Foo: Bar&quot;</span><span style="color: #339933;">,</span>
	oFnc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	 document.<span style="color: #660066;">writeln</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>;
	 <span style="color: #006600; font-style: italic;">//additional object literal</span>
	  <span style="color: #003366; font-weight: bold;">var</span> nestOL <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		nProperty<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Bar: Foo&quot;</span><span style="color: #339933;">,</span>
		nFnc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello World! in alert prompt!&quot;</span><span style="color: #009900;">&#41;</span>;	
	   <span style="color: #009900;">&#125;</span>		 
	 <span style="color: #009900;">&#125;</span>;
&nbsp;
	 <span style="color: #000066; font-weight: bold;">return</span> nestOL;
   <span style="color: #009900;">&#125;</span>	 
 <span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>Since I&#8217;m returning an object-literal from the <font face='courier new'>oFnc</font> function, it causes <font face='courier new'>oFnc</font> to act as function, but aslo an object when it&#8217;s called. To check it&#8217;s function and also, an object when it&#8217;s invoked, just run code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;oFnc is: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">typeof</span> myObject.<span style="color: #660066;">oFnc</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;oFnc() is: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">typeof</span> myObject.<span style="color: #660066;">oFnc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>And you will get the respective results shown in these following screen-shots:</p>
<p><font face='courier new'>oFnc</font> is a function:<br />
<div id="attachment_1916" class="wp-caption aligncenter" style="width: 375px"><a href="http://borisding.com/wp-content/uploads/2010/02/nFnc-function.jpg"><img src="http://borisding.com/wp-content/uploads/2010/02/nFnc-function.jpg" alt="" title="nFnc-function" width="365" height="127" class="size-full wp-image-1916" /></a><p class="wp-caption-text">oFnc is a function</p></div></p>
<p><font face='courier new'>oFnc()</font> is an object when called:<br />
<div id="attachment_1919" class="wp-caption aligncenter" style="width: 375px"><a href="http://borisding.com/wp-content/uploads/2010/02/obFnc-object.jpg"><img src="http://borisding.com/wp-content/uploads/2010/02/obFnc-object.jpg" alt="" title="obFnc-object" width="365" height="127" class="size-full wp-image-1919" /></a><p class="wp-caption-text">oFnc() is an object</p></div></p>
<p>Well, everything is so clear with what we have. Now, we can easily to access the properties/functions on the fly. From the example above, we basically have a nested Object Literal (so-called) and also, defined a &#8216;function&#8217; object which is <font face='courier new'>oFnc</font>. For instance, I wanted to display &#8216;Hello Word!&#8217; in HTML element and followed by &#8216;Hello World! in alert prompt!&#8217; in an alert prompt, here is what I do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">myObject.<span style="color: #660066;">oFnc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">nFnc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Once the line of code is run, it yields the result as follows:<br />
<div id="attachment_1926" class="wp-caption aligncenter" style="width: 570px"><a href="http://borisding.com/wp-content/uploads/2010/02/results.jpg"><img src="http://borisding.com/wp-content/uploads/2010/02/results.jpg" alt="" title="results" width="560" height="228" class="size-full wp-image-1926" /></a><p class="wp-caption-text">Result</p></div></p>
<p>The advantage:<br />
We can make a function to act as function, yet an object as well whenever it&#8217;s called. Besides of this, we can also add the private properties/functions into the defined function, too. This will produce and mimic a &#8216;chaining&#8217; method. In addition, we are doing several tasks at once by calling a single line of code. Enjoy!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2010/02/nested-object-literal-and-making-function-as-object/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Few Things I Used to Do with Firebug</title>
		<link>http://borisding.com/2010/01/few-things-i-used-to-do-with-firebug/</link>
		<comments>http://borisding.com/2010/01/few-things-i-used-to-do-with-firebug/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 10:41:32 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1723</guid>
		<description><![CDATA[Every programmer will make mistakes. Even the most professional and well-versed programmer will write buggy code in the system. Hence, writing code and debugging are said to be a routine task to a programmer.
If you have been involved in Web Development for some time, I believe you have heard about Firebug. In a nutshell, Firebug [...]


Related posts:<ol><li><a href='http://borisding.com/2009/06/faceboxfacebook-style-load-your-page-into-lightbox-on-the-fly/' rel='bookmark' title='Permanent Link: Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!'>Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2010/01/javascript-dynamically-styling-form-elements-in-ie6/' rel='bookmark' title='Permanent Link: JavaScript: Dynamically Styling Form Elements in IE6'>JavaScript: Dynamically Styling Form Elements in IE6</a></li><li><a href='http://borisding.com/2009/03/close-window-without-the-prompt-message-in-ie-7-how/' rel='bookmark' title='Permanent Link: Close Window without the Prompt Message in IE 7, How?'>Close Window without the Prompt Message in IE 7, How?</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Every programmer will make mistakes. Even the most professional and well-versed programmer will write buggy code in the system. Hence, writing code and <a href="http://en.wikipedia.org/wiki/Debugging">debugging</a> are said to be a routine task to a programmer.</p>
<p>If you have been involved in Web Development for some time, I believe you have heard about <a href="http://getfirebug.com/">Firebug</a>. In a nutshell, Firebug is an extension of <a href="http://www.mozilla.com/en-US/">Mozilla Firefox</a> web browser to allow developers to monitor, edit and debug JavaScript, CSS, HTML in their web applications. There are also some add-ons of Firefox to integrate with Firebug, such as <a href="http://developer.yahoo.com/yslow/">YSlow</a> and <a href="http://stevesouders.com/hammerhead/">Hammerhead</a> etc. So, what I used to do with Firebug? Well, I am going to list out some of the features I personally like the most in Firebug. </p>
<p>Before getting started, here is the info of  Firefox and Firebug on my machine:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Firebug: Version 1.5.0
Firefox's user-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6</pre></div></div>

<p><b>1) Debugging JavaScript </b><br />
If there was an error occurred in JavaScript, I will always use Firebug to check with my code. To check JS code, we have to make sure &#8216;Show JavaScript Errors&#8217; has been checked in &#8216;Console&#8217; tab. However, it should be checked by default once we got Firebug installed. Let&#8217;s consider the simple JS code as follows, which will generate an error after it&#8217;s run:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> <span style="color: #003366; font-weight: bold;">function</span> displayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> message <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Hello! Nice to meet you.&quot;</span>;
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span>;
&nbsp;
 displayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Apparently, there was missing a closing braces of the &#8216;displayHello&#8217; function and causing an error. If I&#8217;m running the code snippet by turning on Firebug, I should be able to see the following message under &#8216;Console&#8217; tab, as shown in this screen-shot:<br />
<div id="attachment_1765" class="wp-caption aligncenter" style="width: 523px"><a href="http://borisding.com/wp-content/uploads/2010/01/bug1.gif"><img src="http://borisding.com/wp-content/uploads/2010/01/bug1.gif" alt="" title="bug1" width="513" height="240" class="size-full wp-image-1765" /></a><p class="wp-caption-text">JS error</p></div><br />
By clicking on <font face='courier new' color='green'>displayHello();\n</font>, I will be brought to the code where it halted at run time. So, I can quickly know where I should rectify the code and getting problem fixed.</p>
<p><b>2) Dealing with Ajax</b><br />
Ajax has been widely used and quite common in <a href="http://en.wikipedia.org/wiki/Web_2.0">Web 2.0</a>. Sometimes, we will have to check both front-end and back-end and making sure there is no error in our code. Here is an example of Ajax works together with PHP. Say, I&#8217;m making Ajax call which is working perfectly, however, I encountered a problem and failed to retrieve result via the Ajax call. I can do a quick check on the result that sent back to the front-end in such situation, through Firebug debugger. For instance, I have made a <a href="http://api.jquery.com/jQuery.get/">jQuery&#8217;s get Ajax</a> call and the function is working fine without any error, as follows:</p>
<p><b>Ajax call (with no error)</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> <span style="color: #003366; font-weight: bold;">function</span> displayHelloAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>action<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> result <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> result <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  displayHelloAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Unfortunately, I have gotten an error when &#8216;hello.php&#8217; is called via Ajax and causing me unable to get the expected result in alert prompt. So, what should I do? Definitely, I will have to check on the back-end code. For example, below is the simple PHP code I&#8217;m running on, but with error:</p>
<p><b>PHP (with error)</b></p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;action&quot;</span><span style="color: #009900;">&#93;</span>;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$action</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$action</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;display&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello! Nice to meet you.&quot;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Once I executed the code with Firebug is turned on, I will see the error message under the &#8216;Console&#8217; tab:<br />
<div id="attachment_1800" class="wp-caption aligncenter" style="width: 525px"><a href="http://borisding.com/wp-content/uploads/2010/01/bug2.gif"><img src="http://borisding.com/wp-content/uploads/2010/01/bug2.gif" alt="" title="bug2" width="515" height="243" class="size-full wp-image-1800" /></a><p class="wp-caption-text">PHP error</p></div><br />
When Ajax call is completed (200 is returned), I can expand the GET URL status and to view the result of the back-end, under &#8216;Response&#8217; tab. So in this case, the error is obviously caused by PHP code, whereas I have missed out the semicolon (;) after the echo command. From there, I can instantly know what went wrong with my code and where to correct it. </p>
<blockquote><p>
Note: In this case, however, you still be able to see the same error message in alert prompt. This example emphasizes debugging via Firebug.
</p></blockquote>
<p><b>3) HTML Layout</b><br />
If text editor or <a href="http://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> we use does not support design view, we may take the advantage of Firebug to trace the specific HTML elements from our web page, too.<br />
<div id="attachment_1820" class="wp-caption aligncenter" style="width: 523px"><a href="http://borisding.com/wp-content/uploads/2010/01/layout1.gif"><img src="http://borisding.com/wp-content/uploads/2010/01/layout1.gif" alt="" title="layout1" width="513" height="320" class="size-full wp-image-1820" /></a><p class="wp-caption-text">HTML Layout</p></div><br />
By selecting &#8216;HTML&#8217; mode, I&#8217;m basically allowed to see the exact HTML layout of my current web page. For instance, I may expand the &#8216;body&#8217; tag and see all the elements that reside in that particular tag. The layout will be in coloured overlay when HTML element is selected. Therefore, I&#8217;m able to know which element should go to which design or layout.</p>
<p><b>4) Web Performance</b><br />
If you are fussy with your works, and particular with the speed and network of your web page, we can use Firebug to check with the speed of page loading. Firebug has come out with &#8216;Net&#8217; option to enable us to see the total of time (in milliseconds) to load a file when it is invoked. So, most of time, I will use this option to check the speed of my site and evaluate the time is taken for loading a file such as image, JS file etc. And so, appropriate adjustment might be taken if it is taking too much time to load that particular file. Here is an example of the screen-shot:</p>
<p><a href="http://borisding.com/wp-content/uploads/2010/01/performance.gif"><img src="http://borisding.com/wp-content/uploads/2010/01/performance.gif" alt="Performance" title="performance" width="515" height="320" class="aligncenter size-full wp-image-1832" /></a></p>
<p>Once this option is turned on, I will be able to see all files that is being loaded and total of time that was taken for downloading it. If I only want to check on, say JavaScript files, I can selectively choose &#8216;JS&#8217; and then I&#8217;m able to get the result for JS files only. Likewise, if I wanted to check the response from the server, I can just simply expand the URL and view it under &#8216;Response&#8217; tab, as well.</p>
<p><b>So, what else?</b><br />
The features I have mentioned above are just some good parts of Firebug that I frequently use in my projects. You are allowed to do more things by using Firebug, such as setting breakpoint, editing HTML/CSS straight from Firebug, inspecting DOM elements and so on. There are lots of debugger can be found online, Firebug is only one of the best and highly recommended. Which debugger tool should be used really depends on individual preference. If you want to know more, just check out on <a href="http://getfirebug.com/">Firebug&#8217;s official site</a>.</p>


<p>Related posts:<ol><li><a href='http://borisding.com/2009/06/faceboxfacebook-style-load-your-page-into-lightbox-on-the-fly/' rel='bookmark' title='Permanent Link: Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!'>Facebox(Facebook-Style): Load Your Page into Lightbox on the Fly!</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2010/01/javascript-dynamically-styling-form-elements-in-ie6/' rel='bookmark' title='Permanent Link: JavaScript: Dynamically Styling Form Elements in IE6'>JavaScript: Dynamically Styling Form Elements in IE6</a></li><li><a href='http://borisding.com/2009/03/close-window-without-the-prompt-message-in-ie-7-how/' rel='bookmark' title='Permanent Link: Close Window without the Prompt Message in IE 7, How?'>Close Window without the Prompt Message in IE 7, How?</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2010/01/few-things-i-used-to-do-with-firebug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript: Dynamically Styling Form Elements in IE6</title>
		<link>http://borisding.com/2010/01/javascript-dynamically-styling-form-elements-in-ie6/</link>
		<comments>http://borisding.com/2010/01/javascript-dynamically-styling-form-elements-in-ie6/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 16:30:05 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1639</guid>
		<description><![CDATA[Most of the time, Web Developers and Designers will have to make sure their JavaScript and CSS are working in different browsers and versions. If you have been using CSS2 selector input[type='text'] for styling all form text-fields in your web page, you may have already known that it doesn&#8217;t work in IE6 and prior versions [...]


Related posts:<ol><li><a href='http://borisding.com/2009/01/implement-form-serialization-via-javascript/' rel='bookmark' title='Permanent Link: Implement Form Serialization via JavaScript'>Implement Form Serialization via JavaScript</a></li><li><a href='http://borisding.com/2009/01/get-rid-of-using-eval-to-dynamically-update-form-field/' rel='bookmark' title='Permanent Link: Get Rid of Using eval() to Dynamically Update Form Field'>Get Rid of Using eval() to Dynamically Update Form Field</a></li><li><a href='http://borisding.com/2009/01/create-a-simple-lightbox/' rel='bookmark' title='Permanent Link: Create a Simple LightBox'>Create a Simple LightBox</a></li><li><a href='http://borisding.com/2009/03/watch-out-when-deleting-all-rows-from-a-table-via-javascript/' rel='bookmark' title='Permanent Link: Watch out when deleting all Rows from a Table via JavaScript'>Watch out when deleting all Rows from a Table via JavaScript</a></li><li><a href='http://borisding.com/2009/01/nifty-corners/' rel='bookmark' title='Permanent Link: Nifty Corners'>Nifty Corners</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Most of the time, Web Developers and Designers will have to make sure their JavaScript and CSS are working in different browsers and versions. If you have been using <a href="http://www.w3.org/TR/CSS2/selector.html">CSS2 selector</a> <font face='courier new'>input[type='text']</font> for styling all form text-fields in your web page, you may have already known that it doesn&#8217;t work in IE6 and prior versions of IE, say IE5 and so on. </p>
<p>For example, I wanted to apply the following CSS to all text-fields in my web page, here is how it would look like:</p>
<p><b>CSS</b></p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'text'</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccccff</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><b>HTML Form</b></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;label&gt;Form 1&lt;/label&gt;
&lt;form name=&quot;form1&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;f1-input1&quot; size=&quot;30&quot;/&gt;
&lt;input type=&quot;text&quot; name=&quot;f1-input2&quot; size=&quot;30&quot;/&gt;
&lt;/form&gt;
&nbsp;
&lt;label&gt;Form 2&lt;/label&gt;
&lt;form name=&quot;form2&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;f2-input1&quot; size=&quot;30&quot;/&gt;
&lt;input type=&quot;text&quot; name=&quot;f2-input2&quot; size=&quot;30&quot;/&gt;
&lt;input type=&quot;text&quot; name=&quot;f2-input3&quot; size=&quot;30&quot;/&gt;
&lt;/form&gt;</pre></div></div>

<p>Well, as what you have expected, it will apply to all text-fields and set the &#8216;border&#8217; attributes to respective text-field elements. It basically works fine in Firefox, Chrome, Safari, Opera and IE7/8. Unfortunately, it is NOT working in IE6 and below.</p>
<p><b>The Trick Is?</b><br />
So, what should we do for dealing with that issue? Perhaps, the simplest method is to use CSS class method. Whereas, change the CSS to something as follows:</p>
<p><b>CSS</b></p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"> <span style="color: #6666ff;">.iTextStyle</span> <span style="color: #00AA00;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccccff</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>and, add the &#8216;class&#8217; attribute in every single input text-field like so:</p>
<p><b>HTML</b></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;text&quot; name=&quot;f1-input1&quot; size=&quot;30&quot; class=&quot;iTextStyle&quot;/&gt;
&lt;!-- more text-fields below --&gt;</pre></div></div>

<p>OK! Everything seems working fine now. Nevertheless, it will be annoying to add &#8216;class&#8217; attribute accordingly if you are having number of forms in your web page. So, this method is only applicable if you are having a form with few elements.</p>
<p>The better way is, we can dynamically style all text-field elements by having a little help with JavaScript. Here is an example of JS code I have written:</p>
<p><b>JavaScript</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//count number of form</span>
 <span style="color: #003366; font-weight: bold;">var</span> numForm <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span>.<span style="color: #660066;">length</span>;
 <span style="color: #006600; font-style: italic;">//loop over all forms and form[] elements</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> 0; i <span style="color: #339933;">&lt;</span> numForm; i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//count number of form's elements</span>
	<span style="color: #003366; font-weight: bold;">var</span> numInput <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">elements</span>.<span style="color: #660066;">length</span>;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> 0; j <span style="color: #339933;">&lt;</span> numInput; j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	  <span style="color: #006600; font-style: italic;">//get text element only from current form</span>
	   <span style="color: #003366; font-weight: bold;">var</span> typeElement <span style="color: #339933;">=</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>;
	  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> typeElement <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    <span style="color: #006600; font-style: italic;">//apply class to particular element</span>
		document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;iTextStyle&quot;</span>;
	  <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>   
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically, the code snippet of JS will count the number of form in your web page and, particularly iterate over elements from the form, as well as the type of each element is being checked. As long as the text-field element is encountered, we then apply the related class name to the matched element.</p>
<p>The advantage here is, you do not need to care the number of form/element in your web page. Yet, it will apply the CSS to all matched elements, even in IE6 and below.</p>
<blockquote><p>
NOTE:<br />
1) You can create a JS function, say &#8217;styleTextElement&#8217; and put the code in that function; call it whenever you want.<br />
2) The code should be executed AFTER all forms in your web page. Otherwise, it will not work.<br />
3) You may check on the version of IE and only render the code when IE6 and prior versions are used for browsing the web page. For more recent versions, we can simply use CSS selector method instead.
</p></blockquote>


<p>Related posts:<ol><li><a href='http://borisding.com/2009/01/implement-form-serialization-via-javascript/' rel='bookmark' title='Permanent Link: Implement Form Serialization via JavaScript'>Implement Form Serialization via JavaScript</a></li><li><a href='http://borisding.com/2009/01/get-rid-of-using-eval-to-dynamically-update-form-field/' rel='bookmark' title='Permanent Link: Get Rid of Using eval() to Dynamically Update Form Field'>Get Rid of Using eval() to Dynamically Update Form Field</a></li><li><a href='http://borisding.com/2009/01/create-a-simple-lightbox/' rel='bookmark' title='Permanent Link: Create a Simple LightBox'>Create a Simple LightBox</a></li><li><a href='http://borisding.com/2009/03/watch-out-when-deleting-all-rows-from-a-table-via-javascript/' rel='bookmark' title='Permanent Link: Watch out when deleting all Rows from a Table via JavaScript'>Watch out when deleting all Rows from a Table via JavaScript</a></li><li><a href='http://borisding.com/2009/01/nifty-corners/' rel='bookmark' title='Permanent Link: Nifty Corners'>Nifty Corners</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2010/01/javascript-dynamically-styling-form-elements-in-ie6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Few Things to Note When Authoring jQuery Plugin</title>
		<link>http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/</link>
		<comments>http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:03:04 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Agotcha!]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[QShout]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1506</guid>
		<description><![CDATA[I was spending my time on authoring jQuery plugin, in past few days. This is the very first jQuery plugin I have worked on, which is based on jQuery core library, version 1.3.2. In a nutshell, I have adopted my previous work, Agotcha! to come out with a simple shoutbox widget, yet it&#8217;s supported by [...]


Related posts:<ol><li><a href='http://borisding.com/2010/02/tutorial-how-to-write-your-own-jquery-plugins/' rel='bookmark' title='Permanent Link: Tutorial: How to Write Your Own jQuery Plugins?'>Tutorial: How to Write Your Own jQuery Plugins?</a></li><li><a href='http://borisding.com/2008/12/s3slider-fading-your-images-with-jquery-plugin/' rel='bookmark' title='Permanent Link: s3slider: Fading your images with jQuery plugin'>s3slider: Fading your images with jQuery plugin</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2009/01/john-resig-javascript-jquery/' rel='bookmark' title='Permanent Link: John Resig: JavaScript &#038; jQuery'>John Resig: JavaScript &#038; jQuery</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>I was spending my time on authoring jQuery plugin, in past few days. This is the <a href="http://qshout.borisding.com">very first jQuery plugin</a> I have worked on, which is based on <a href="http://jquery.com">jQuery core library</a>, version 1.3.2. In a nutshell, I have adopted my previous work, <a href="http://borisding.com/agotcha">Agotcha!</a> to come out with a simple shoutbox widget, yet it&#8217;s supported by jQuery library. The main objective of this work is to challenge myself and therefore, having deeper understanding on how jQuery works.</p>
<p>Well, after I have got this plugin done, from what I have experienced, here are few important things I personally felt that every developer should pay attention when authoring a jQuery plugin.</p>
<p><b>1) Passing jQuery object to Anonymous function</b><br />
First and foremost, before you start putting any code of yours, it&#8217;s always a good idea to pass the jQuery object to an anonymous function, as suggested in <a href="http://docs.jquery.com/Plugins/Authoring">jQuery&#8217;s Plugins/Authoring page</a> (and also mentioned in other developers&#8217; blogs). If you have been coding jQuery before, I believe most of the time you will use jQuery&#8217;s alias object- &#8220;$&#8221; the dollar sign for calling a method/function. Why to do so? something you need to keep in mind is, there are thousands of libraries are available and can be found online and they maybe using the same &#8220;$&#8221; object, the well-known library is <a href="http://www.prototypejs.org/">Prototype</a>. So to avoid any object conflicts, you should always wrap jQuery object and pass it to an anonymous function, so that you are allowed to use the &#8220;$&#8221; alias without worrying conflict with other libraries, especially when a developer he or she is using more than one JavaScript library. Here how it does look like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #006600; font-style: italic;">//your code is going from here...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>As you may have already known, this anonymous function will be invoked immediately once the page is loaded, by passing &#8216;jQuery&#8217; object and assigned to a &#8216;$&#8217; dollar sign, then only proceed to your own code. By using this trick, you can always use $ object within the function without worrying the conflicts.</p>
<p><b>2) Methods or Functions?</b><br />
If you have read the jQuery&#8217;s Plugins/Authoring page, you may have already known that any methods should be attached to jQuery.fn object; and all functions to the jQuery object. Well, what all these mean to us? Most of the articles or tutorials have done a good job and also shown the way of authoring a jQuery plugin, but most of the time, it seldom mentions <em>when</em> and <em>why</em> we should use that in our own plugins and I do believe, some of the developers are confused with these when trying to get started with jQuery plugin development.</p>
<p><u>When to use method and attach it to jQuery.fn or $.fn?</u><br />
From my understanding, you will only attach a method to jQuery.fn when the same method is being used more than one time, and more importantly, it&#8217;s attached to jQuery.fn when you wanted to use it for &#8216;chaining&#8217; purpose. In other words, you should always return it (jQuery object) once the process is done and to allow it&#8217;s used together with the jQuery&#8217;s methods from the core library. For instance, you wanted to add css after your method was called. All we do is put it as chaining pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">  $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">myOwnMethod</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// 'this' in each() function is always referred to current HTML's element object...</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;this is my text..&quot;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;  
 <span style="color: #009900;">&#125;</span>;
&nbsp;
 $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div#myId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">myOwnMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#ccccff'</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>From the snippet above, you&#8217;re applying your own method (myOwnMethod) to an HTML&#8217;s div element with its id, &#8216;myId&#8217; by inserting text and then chained it with jQuery&#8217;s css() method and set the div&#8217;s background colour as &#8216;#ccccff&#8217;. The core concept behind is, or something you must always keep in mind is the &#8216;this&#8217; object:</p>
<blockquote><p>
i) &#8216;this&#8217; tied with each() method, this.each():<br />
 &#8211; The word, &#8216;this&#8217; here is the jQuery&#8217;s object, as in the &#8216;$&#8217; dollar sign object, which is being used to call the each() method from the core library.<br />
ii) &#8216;this&#8217; <u>inside</u> each() method:<br />
-  The &#8216;this&#8217; object here is always referred to the current HTML&#8217;s element object. In this example is, the div element with &#8216;myId&#8217;.
</p></blockquote>
<p>With this solid concept in mind, you are able to create any methods of yours that will be used for chaining with current jQuery&#8217;s core methods and utilities.</p>
<p><u>How about the &#8216;functions&#8217;?</u><br />
Personally, I feel that creating functions is much more easier than methods, as what I have explained, previously. Functions in jQuery are basically created and tied directly with jQuery&#8217;s object: &#8216;jQuery&#8217; and its alias, &#8216;$&#8217; dollar sign. In other words, whenever you need it, just call it directly via jQuery&#8217;s object. Most of the time, it&#8217;s also called as &#8216;public method&#8217; which will be directly called for getting a result. The way we define the function is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> $.<span style="color: #660066;">myOwnFunction</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//put your code here...</span>
<span style="color: #009900;">&#125;</span>;
<span style="color: #006600; font-style: italic;">//to call it</span>
 $.<span style="color: #660066;">myOwnFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Pretty straight-forward, right? What you need to do is putting everything inside &#8216;myOwnFunction&#8217; function that attached to jQuery object and then call it straight via its object.</p>
<p><u>Which one should be used?</u><br />
To me, it really depends on what you are going to do with your plugin. You may just either use method or function, or even both of them in your jQuery plugin. But, always ask yourself what is the purpose of creating this method/function? Would it be used for chaining? With this concept in your mind, you are able to create jQuery plugins, on the fly.</p>
<p><b>3) Using jQuery, $.extend()</b><br />
You must always understand and clear with a method or function you are going to use, before you&#8217;re getting frustrated of it. So, if you do not know what is jQuery&#8217;s extend() used for, <a href="http://docs.jquery.com/Core/jQuery.extend">here you go</a>. Most of the time, you will need $.extend() to extend jQuery&#8217;s object. Say, I have already created a function, named as &#8216;myOwnFunction&#8217; and I wanted to extend it with an object-literal and to allow me to access the properties from the object-literal, via &#8216;myOwnFunction&#8217;. Here is what I will do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> $.<span style="color: #660066;">myOwnFunction</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//my code here...</span>
<span style="color: #009900;">&#125;</span>;
<span style="color: #006600; font-style: italic;">//extend the function and make it as 'object function'</span>
 $.<span style="color: #660066;">myOwnFunction</span>  <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">myOwnFunction</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   name1<span style="color: #339933;">:</span> value1<span style="color: #339933;">,</span>
   func1<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//some block code...</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #006600; font-style: italic;">//more functions/properties here...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>So, what was just happening? By using $.extend(), I have actually made the &#8216;myOwnFunction&#8217; as an object, too! That means, I can now not only call the function via $ object, and at the same time, I can &#8216;extend&#8217; the function to play as object as well, in order to access the properties/ functions from the object-literal.</p>
<p>Sometimes, you may want to allow user inputs options apart from the default settings in your plugin. Here, $.extend() will take the place, again. For instance:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">  $.<span style="color: #660066;">myOwnFunction</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> userOptions <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//the default settings...</span>
    myDefaultSetting <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
     name1 <span style="color: #339933;">:</span> value1<span style="color: #339933;">,</span>
     nameN <span style="color: #339933;">:</span> valueN
    <span style="color: #009900;">&#125;</span>;
  <span style="color: #006600; font-style: italic;">//merge it with default or, overwrite when different value assigned...</span>
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> userOptions  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      myDefaultSetting <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> myDefaultSetting<span style="color: #339933;">,</span> userOptions <span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>The example above is to allow users provide the options apart from using yours default values. Don&#8217;t you think that it will be more user-friendly plugin, huh? Give applause to jQuery!</p>
<p><b>4) The $(&#8216;id&#8217;).find() method</b><br />
Last but not least, one more important thing we should take note is always looking for the correct and matched to the right HTML element. From what I have experienced, sometimes we might forget or overlook to look up the correct HTML elements. It will be a pitfall if we never look for an HTML element in detailed, although everything seems working pretty fine. Well, just consider of this example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&gt;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #009900;">&#41;</span>;  
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&gt;li&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;pink&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>From the code, everything seems like working pretty well, where we have tried to set the respective background colours to the &#8216;ul&#8217; tag and its &#8216;li&#8217; in div element, whereas, blue and pink colours. Yet, if you try to duplicate the same div element with &#8216;ul&#8217; tag resides in it, and then run the code again you will see that, it will apply to all &#8216;ul&#8217; elements! Think further a little, how about if the developers out there are using your plugin, but it ruins the whole layout in case they are using &#8216;ul&#8217; in the div element, too!</p>
<p>So, whenever possible, try to use <a href="http://docs.jquery.com/Traversing/find">jQuery&#8217;s find() method</a> to look up more specific HTML element. This will avoid the problem happening as I mentioned just now. OK, we tweak the code a little as such:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div#myId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#ccccff&quot;</span><span style="color: #009900;">&#41;</span>;  
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div#myId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&gt;li&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;pink&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now, we are able to look for more specific HTML element without affecting user&#8217;s interface. If you are putting the code in $.each() function, always use &#8216;this&#8217; instead since it&#8217;s always referred to the current HTML element&#8217;s object. The benefit is, when you want to change the div&#8217;s id, you do not need to change it in everything single line of code and therefore, save your time from there.</p>
<p>Hope this post will help someone who is going to author his/her own jQuery plugin(s).</p>


<p>Related posts:<ol><li><a href='http://borisding.com/2010/02/tutorial-how-to-write-your-own-jquery-plugins/' rel='bookmark' title='Permanent Link: Tutorial: How to Write Your Own jQuery Plugins?'>Tutorial: How to Write Your Own jQuery Plugins?</a></li><li><a href='http://borisding.com/2008/12/s3slider-fading-your-images-with-jquery-plugin/' rel='bookmark' title='Permanent Link: s3slider: Fading your images with jQuery plugin'>s3slider: Fading your images with jQuery plugin</a></li><li><a href='http://borisding.com/2009/05/jquery-implement-ajax-with-ajax/' rel='bookmark' title='Permanent Link: jQuery: Implement Ajax with $.ajax()'>jQuery: Implement Ajax with $.ajax()</a></li><li><a href='http://borisding.com/2009/06/create-fancy-loading-effect-with-jquery/' rel='bookmark' title='Permanent Link: Create Fancy Loading Effect with jQuery'>Create Fancy Loading Effect with jQuery</a></li><li><a href='http://borisding.com/2009/01/john-resig-javascript-jquery/' rel='bookmark' title='Permanent Link: John Resig: JavaScript &#038; jQuery'>John Resig: JavaScript &#038; jQuery</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2010/01/few-things-to-notice-when-authoring-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indent Style in JavaScript</title>
		<link>http://borisding.com/2009/12/indent-style-in-javascript/</link>
		<comments>http://borisding.com/2009/12/indent-style-in-javascript/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 15:23:48 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1412</guid>
		<description><![CDATA[Few weeks ago, I have seen a video clip titled with &#8216;JavaScript: The Good Parts&#8216; on YouTube, presented by Douglas Crockford, where he did mention the way of writing JS code. I was agreed with what he said, the way to govern indentation of blocks of code shouldn&#8217;t be subjective. Different developers or programmers have [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago, I have seen a video clip titled with &#8216;<a href="http://www.youtube.com/watch?v=hQVTIJBZook">JavaScript: The Good Parts</a>&#8216; on <a href="http://youtube.com">YouTube</a>, presented by <a href="http://en.wikipedia.org/wiki/Douglas_Crockford">Douglas Crockford</a>, where he did mention the way of writing JS code. I was agreed with what he said, the way to govern indentation of blocks of code shouldn&#8217;t be subjective. Different developers or programmers have their own preferred of code indentation when writing program. However, in JavaScript, you are highly encouraged to use <a href="http://en.wikipedia.org/wiki/Indent_style#BSD_KNF_style">BSD KNF style</a>, which is also known as &#8216;Kernel Normal Form&#8217;. Why do I say so? I will show you what thing goes wrong later.</p>
<p>I understand that, those programmers who come from C/C++ or C# background, commonly write their code in <a href="http://en.wikipedia.org/wiki/Indent_style#K.26R_style">K&#038;R style</a>, which is typically place the first curly brace at the following line just after the declaration, and ended by another curly brace. However, there is a pitfall when writing this style in JavaScript. Well, without talking much, why not we just take a look at the code, as follows:</p>
<p>Code A:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//ERROR: Silent and returned 'undefined' value</span>
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//some code here...</span>
  <span style="color: #000066; font-weight: bold;">return</span>;
  <span style="color: #009900;">&#123;</span>
    name1<span style="color: #339933;">:</span> value1<span style="color: #339933;">,</span>
    name2<span style="color: #339933;">:</span> value2
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Code B:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//valid, returned object literal notaton</span>
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//some code here...</span>
 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
  name1<span style="color: #339933;">:</span> value1<span style="color: #339933;">,</span>
  name2<span style="color: #339933;">:</span> value2
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>At the first glance, both of the examples look identical and return the same result. Whereas, the declared function will return an object literal notation, in name-value pairs. But, if you look closely, it&#8217;s actually returning two different results. In Code A example, we exit the function immediately when &#8216;return&#8217; is encountered, and therefore return &#8216;nothing&#8217;, because of the object literal {} are located at the next line of &#8216;return&#8217; reserved word. In other words, the following code will not be executed and &#8216;undefined&#8217; value is returned. (I added a semicolon (;) after &#8216;return&#8217; word to make code looks clearer, which should indicate the end of the current line of code.) </p>
<p>Conversely, if you look at the Code B, which is written in BSD KNF style, the code is perfectly executed and an object literal notation is returned. This is because of the first curly brace of the object literal is recognized when we put it just after the &#8216;return&#8217; reserved word. Apparently, it returns a value here; the said object literal.</p>
<p><b>Good Practice</b><br />
So, what is the key point from the above example? The conclusion is, just try make it as a good practice to indent the code by using BSD KNF style, especially in JS. It&#8217;s surely alright if you are using other indentation patterns. Of course, it&#8217;s still valid. But, somehow, in certain circumstances, you might be creating problems or pitfalls without your conscious and getting the unexpected results, just like the case I mentioned previously.</p>
<blockquote><p>
NOTE:<br />
This post is not comparing the pros/cons between &#8216;BSD KNF style&#8217; and &#8216;K&#038;R style&#8217;. The writer just borrowed the terms to make thing clearer and for better explanation.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2009/12/indent-style-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced JS: Having Fun with JavaScript Design Patterns</title>
		<link>http://borisding.com/2009/12/advanced-js-having-fun-with-javascript-design-patterns/</link>
		<comments>http://borisding.com/2009/12/advanced-js-having-fun-with-javascript-design-patterns/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:37:39 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1374</guid>
		<description><![CDATA[Undeniable, a well design pattern is required when writing your own JavaScript libraries or frameworks, that could be used for personal or industry purpose. There are many design patterns can be found online which come together with demo or examples; different people, may have different taste of writing their own code. 
Many developers are talking [...]


Related posts:<ol><li><a href='http://borisding.com/2009/07/javascript-different-methods-of-writing-hello-world/' rel='bookmark' title='Permanent Link: JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;'>JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;</a></li><li><a href='http://borisding.com/2009/11/javascript-testing-on-prototype-together-with-object-literal/' rel='bookmark' title='Permanent Link: JavaScript &#8211; Testing on &#8216;Prototype&#8217; Together with Object Literal'>JavaScript &#8211; Testing on &#8216;Prototype&#8217; Together with Object Literal</a></li><li><a href='http://borisding.com/2009/01/176/' rel='bookmark' title='Permanent Link: JavaScript &#8211; The Object-oriented Programming'>JavaScript &#8211; The Object-oriented Programming</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Undeniable, a well design pattern is required when writing your own JavaScript libraries or frameworks, that could be used for personal or industry purpose. There are many design patterns can be found online which come together with demo or examples; different people, may have different taste of writing their own code. </p>
<p>Many developers are talking about design patterns. Well, to me, it really doesn&#8217;t matter what kind of design pattern you are using, as long as it&#8217;s readable, well-organized and adaptable as well as maintainable, it&#8217;s all fine.</p>
<p>I have recently played around with JS design pattern, as well. When deeper I delve into, JS surprises me more! I could say that, this is the only programming language (so far) can really make me feel excited and willing to spend my time on it. It&#8217;s worth.</p>
<p>So, what is the result I come out? Ok, here is an example I have written, which I consider it could be used for designing library or framework:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span>
 window <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span> || <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>;
 <span style="color: #006600; font-style: italic;">//check on LIB object</span>
 <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> LIB <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;object&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  LIB <span style="color: #339933;">=</span> window;
 <span style="color: #009900;">&#125;</span>
 <span style="color: #006600; font-style: italic;">//define alternate object</span>
 <span style="color: #006600; font-style: italic;">//in case, LIB object is overwritten</span>
  _LIB <span style="color: #339933;">=</span> LIB <span style="color: #339933;">=</span> window.<span style="color: #660066;">LIB</span>;
&nbsp;
<span style="color: #009966; font-style: italic;">/** MODULE ONE **/</span> 
 LIB.<span style="color: #660066;">ModuleOne</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//Add private function(s) in ModuleOne</span>
   <span style="color: #003366; font-weight: bold;">function</span> privateFncOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> localVar <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;this is local variable in 'privateFncOne' funciton, ModuleOne&quot;</span>;
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>localVar<span style="color: #009900;">&#41;</span>;
	<span style="color: #006600; font-style: italic;">/*
	 do more thing from here
	 or, even add closures...
	*/</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">//object literal</span>
   <span style="color: #003366; font-weight: bold;">var</span> coreOne <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
     propertyOne<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is propertyOne's value&quot;</span><span style="color: #339933;">,</span>
	 fncOne<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;This is function resides in object literal from ModuleOne&quot;</span>;
	   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span>;
&nbsp;
       <span style="color: #006600; font-style: italic;">//retrieve object</span>
	   <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> LIB.<span style="color: #660066;">ModuleTwo</span>.<span style="color: #660066;">fncTwo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> o <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    <span style="color: #006600; font-style: italic;">//call function</span>
		o.<span style="color: #660066;">greetFnc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #006600; font-style: italic;">//display text</span>
		 <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">greetText</span><span style="color: #009900;">&#41;</span>;
	   <span style="color: #009900;">&#125;</span>
	 <span style="color: #009900;">&#125;</span>
	 <span style="color: #006600; font-style: italic;">/*
	  a block of code putting here...	 
	 */</span>
   <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//end core</span>
   <span style="color: #000066; font-weight: bold;">return</span> coreOne;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009966; font-style: italic;">/** MODULE TWO **/</span>
 LIB.<span style="color: #660066;">ModuleTwo</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//Add private function(s) in ModuleTwo</span>
  <span style="color: #003366; font-weight: bold;">function</span> privateFncTwo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> localVar <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;this is local variable in 'privateFncTwo' function, ModuleTwo&quot;</span>;
   	<span style="color: #006600; font-style: italic;">/*
	 do more thing from here
	 or, even add closures...
	*/</span>
  <span style="color: #009900;">&#125;</span>  
   <span style="color: #006600; font-style: italic;">//object literal</span>
   <span style="color: #003366; font-weight: bold;">var</span> coreTwo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
     propertyOne<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is propertyOne's value&quot;</span><span style="color: #339933;">,</span>
	 fncOne<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;This is function in object literal from ModuleOne&quot;</span>;
	   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span>;
	 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	 fncTwo<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	  <span style="color: #006600; font-style: italic;">//return object literal</span>
	  <span style="color: #000066; font-weight: bold;">return</span> LIB.<span style="color: #660066;">ModuleOne</span>.<span style="color: #660066;">fncOne</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">objectLiteral</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	    greetText <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Have a nice day!&quot;</span><span style="color: #339933;">,</span>
		greetFnc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;How is your day?&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #009900;">&#125;</span>
	  <span style="color: #009900;">&#125;</span>
	 <span style="color: #009900;">&#125;</span>
	 <span style="color: #006600; font-style: italic;">/*
	  a block of code putting here...	 
	 */</span>
   <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//end core</span>
   <span style="color: #000066; font-weight: bold;">return</span> coreTwo;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">//EXAMPLE OF CALLING FUNCTION</span>
<span style="color: #006600; font-style: italic;">/**
Result:
This is function resides in object literal from ModuleOne
How is your day?
Have a nice day!
**/</span>
 LIB.<span style="color: #660066;">ModuleOne</span>.<span style="color: #660066;">fncOne</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Basically, the core idea is to create an object, in this case, &#8216;LIB&#8217; with two main modules, respectively. As you can find, there are private function(s), object literals or even to allow you add more functions/closures (or, whatever terminology you have? |o|), just inside the respective modules. </p>
<p>From the example, I have also shown you how to call one of the functions that resides in the first module. And therefore, retrieve following data that returned from the second module and so on. Some developers might call it as &#8216;<a href="http://ajaxian.com/archives/a-javascript-module-pattern">Module Pattern</a>&#8216;, which was coined by <a href="http://www.crockford.com/">Douglas Crockford</a>, from <a href="http://www.yahoo.com">Yahoo!</a> Just somehow, the code might look a bit bulky.</p>
<p>Again, JavaScript is NOT a toy!</p>
<p>Should you have any feedback or comments, just feel free to post here for discussion. Happy JavaScripting!</p>


<p>Related posts:<ol><li><a href='http://borisding.com/2009/07/javascript-different-methods-of-writing-hello-world/' rel='bookmark' title='Permanent Link: JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;'>JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;</a></li><li><a href='http://borisding.com/2009/11/javascript-testing-on-prototype-together-with-object-literal/' rel='bookmark' title='Permanent Link: JavaScript &#8211; Testing on &#8216;Prototype&#8217; Together with Object Literal'>JavaScript &#8211; Testing on &#8216;Prototype&#8217; Together with Object Literal</a></li><li><a href='http://borisding.com/2009/01/176/' rel='bookmark' title='Permanent Link: JavaScript &#8211; The Object-oriented Programming'>JavaScript &#8211; The Object-oriented Programming</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2009/12/advanced-js-having-fun-with-javascript-design-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing Object-Literal&#8217;s Property to External Function</title>
		<link>http://borisding.com/2009/12/passing-object-literals-property-to-external-function/</link>
		<comments>http://borisding.com/2009/12/passing-object-literals-property-to-external-function/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 08:52:10 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1311</guid>
		<description><![CDATA[Recently, I have been coding JavaScript heavily in Object-Literal manner. As you may have already known, this is one of the most popular and modern methods of writing JS code. You are allowed to access any property and method via object, at anytime and anywhere. It is possible to pass object literal&#8217;s properties to the [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been coding JavaScript heavily in <a href="http://www.dyn-web.com/tutorials/obj_lit.php">Object-Literal</a> manner. As you may have already known, this is one of the most popular and modern methods of writing JS code. You are allowed to access any property and method via object, at anytime and anywhere. It is possible to pass object literal&#8217;s properties to the external function(s). When I say &#8216;external&#8217;, it means the function that&#8217;s not residing in the curly brackets {}. Consider the below code as an example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>  
  displayName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> personName <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> personName;
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #009900;">&#41;</span>;   
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">displayNameAgain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
person.<span style="color: #660066;">displayNameAgain</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> myName <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span>;
 <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Once again, called my name: &quot;</span> <span style="color: #339933;">+</span> myName <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//call to display person's name</span>
person.<span style="color: #660066;">displayName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Boris Ding&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>If you are one of the geeks writing JS in this manner, the code shown as above should be looking fine to you. As you can see, there was a &#8216;name&#8217; property and &#8216;displayName&#8217; function, with one argument, assigned to the object, whereas &#8216;person&#8217; with the mentioned property and function reside in the curly brackets {}</p>
<p>Perhaps, you have also noticed that, there was an external function, too. And, I named it as &#8216;displayNameAgain&#8217; that has been created via the &#8216;person&#8217; object, as well. The purpose of having this function is to retrieve the person&#8217;s property, as in the &#8216;name&#8217; and being displayed whenever it&#8217;s called.</p>
<p>Well, how to make all things work together?</p>
<p>When the &#8216;displayName&#8217; function is called, by passing a value, for this case is my name &#8216;Boris Ding&#8217; something like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">person.<span style="color: #660066;">displayName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Boris Ding&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>As what you have expected, I actually called the &#8216;displayName&#8217; function via person object. Before I&#8217;m prompted with the alert pop-up, the &#8216;name&#8217; property has initially been assigned to the value I have passed in, whereas via the &#8216;personName&#8217; argument. Needless to say, the &#8216;name&#8217; property is now holding the value of &#8216;Boris Ding&#8217; that assigned to it and therefore, I got the alert pop-up with &#8216;Boris Ding&#8217; displayed.<br />
<center><br />
<img src="http://borisding.com/wp-content/uploads/2009/12/alert1.jpg"/><br />
</center><br />
Once I was prompted with the alert pop-up, the next line is calling the next function, which is &#8216;displayNameAgain&#8217; that previously created as external function. The function looks something like so:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">person.<span style="color: #660066;">displayNameAgain</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> myName <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span>;
 <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Once again, called my name: &quot;</span> <span style="color: #339933;">+</span> myName <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Within this function, I have actually assigned the &#8216;name&#8217; property&#8217;s value to the local variable, &#8216;myName&#8217;. Since <font face='Courier New'>this</font> word is always referring to the current object, (&#8216;person&#8217; in this case), it should be done like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> <span style="color: #003366; font-weight: bold;">var</span> myName <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span>;</pre></div></div>

<p>As I mentioned, since &#8216;displayNameAgain&#8217; was created via &#8216;person&#8217; object and <font face='Courier New'>this</font> reserved word refers to the current object as well, so, to call the function, this is the code how we do it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">displayNameAgain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>And it yields the result, another alert pop-up as follows:<br />
<center><br />
<img src="http://borisding.com/wp-content/uploads/2009/12/alert2.jpg"/><br />
</center></p>
<p><strong>How it works?</strong><br />
As you have seen, the local variable &#8216;myName&#8217; is now holding the same value with &#8216;name&#8217; property in the curly brackets. How could it be? The reason is fairly simple. As mentioned, &#8216;name&#8217; is the property of &#8216;person&#8217; object. As, I have defined &#8216;displayNameAgain&#8217; function via &#8216;person&#8217; object, too. Just the difference is, I&#8217;m putting it <i>outside</i> of the {} that assigned to &#8216;person&#8217; object. So, it&#8217;s possible to pass the &#8216;name&#8217; property&#8217;s value to the local variable, &#8216;myName&#8217; that resides in the &#8216;displayNameAgain&#8217; function. Likewise, for calling the &#8216;displayNameAgain&#8217; function, you may also call it by using &#8216;<font face='Courier New'>person.displayNameAgain()</font>&#8216; or, preferably, in this way: &#8216;<font face='Courier New'>this.displayNameAgain()</font>&#8216;, whereas, <font face='Courier New'>this</font> refers to the current object, which is &#8216;person&#8217; in this example.</p>
<p>I hope this simple example will make things become clearer.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2009/12/passing-object-literals-property-to-external-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Security Practices in Web Development &#8211; Part II</title>
		<link>http://borisding.com/2009/11/some-security-practices-in-web-development-part-ii/</link>
		<comments>http://borisding.com/2009/11/some-security-practices-in-web-development-part-ii/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 12:07:31 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1183</guid>
		<description><![CDATA[Not long ago, I had briefly mentioned some security practices that may need to be applied in Web Development. This post, I&#8217;m going to list out some other good practices that developers may need to follow when writing their code, in order to avoid malicious attacks.
@1: SQL Injection
Most of the developers are aware of this [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p><a href="http://borisding.com/2009/11/some-security-practices-in-web-development-part-i/">Not long ago</a>, I had briefly mentioned some security practices that may need to be applied in Web Development. This post, I&#8217;m going to list out some other good practices that developers may need to follow when writing their code, in order to avoid malicious attacks.</p>
<p>@1: SQL Injection<br />
Most of the developers are aware of this issue, however, most of the time, some of the developers are still repeating the poor manner of writing SQL statements when querying data from the database. In case you don&#8217;t know what SQL Injection is all about, <a href="http://en.wikipedia.org/wiki/SQL_injection">according to Wiki</a>, the easiest way to explain SQL injection is as follows:</p>
<blockquote><p>
SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application.
</p></blockquote>
<p>This problem occurs is mainly caused by improper inputs filtering and leads to the vulnerability, as in this example written in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;">$sql <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;SELECT * FROM tbl_user WHERE email = '&quot;</span><span style="color: #66cc66;">.</span> $userEmail <span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;'&quot;</span>;</pre></div></div>

<p><font face='courier new'>$userEmail</font> is basically a variable that holds the user&#8217;s email address, that&#8217;s going to be used in SQL statement for querying user particular. Saying that, this variable has never been filtered before going into SQL, a malicious user may assign invalid email address by entering arbitrary string that ruins your SQL statement, for example, assigned &#8220;myemail@testdomain.com&#8217;&#8221; to <font face='courier new'>$userEmail</font> variable, and yields the SQL like so:</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> tbl_user <span style="color: #993333; font-weight: bold;">WHERE</span> email <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'myemail@testdomain.com'</span><span style="color: #ff0000;">';</span></pre></div></div>

<p>If you never filter the inputs before querying, you will get an error instead. What about if you are querying password or some other confidential data? The malicious users are able to retrieve it in this way! So keep in mind that, always escape the string before going into SQL. If you are PHP developer and dealing with MySQL database, you may use <font face='courier new'>mysql_real_escape_string()</font> built in function (preferred), otherwise you may use <font face='courier new'>addslashes()</font>, instead. Alternatively, you may also use PHP&#8217;s magic quotes function, however, this has been removed in PHP6. So, relying on this feature is strongly discouraged. (<a href="http://php.net/manual/en/security.magicquotes.php">Read more</a>)</p>
<p>@2: Password Encryption<br />
A good application should always encrypt users&#8217; password, all the time. This is to add the extra layer of security to protect the privacy of users. Unfortunately, there are still many applications do not encrypt password provided by end users, and being stored as plain text in the database. As I mention &#8216;plain text&#8217;, yes, it&#8217;s the exact password you provided when signing up an account on the particular web application/website and stored as it is.</p>
<p>From developer&#8217;s perspective, (if you claimed yourself as a &#8216;good&#8217; + &#8216;professional&#8217; developer), you should always provide a function to encrypt the confidential data, such as password and so on. PHP itself has already provided number of built in functions to encrypt data, such as: <font face='courier new'>md5()</font>, <font face='courier new'>crypt()</font>, <font face='courier new'>sha1()</font> (for One-Way encryption) and <font face='courier new'>ext/mcrypt</font> (for Two-Way encryption); I had also written a simple PHP Two-Way encryption tool &#8211; <a href="http://borisding.com/projects/bpencryptor/">BPEncryptor</a>, as recently announced in this <a href="http://borisding.com/2009/11/introduction-to-bpencryptor-a-two-way-encryption-function/">post</a>. However, it does not support <a href="http://en.wikipedia.org/wiki/Unicode_character">unicode character</a> at this time.</p>
<p>@3: PHP File Settings<br />
If you are PHP developer, you may need to pay attention in PHP settings. The most common settings should correctly be set, for the sake of security concerns. Some of the settings need to be checked in php.ini file such as: Disable <font face='courier new'>allow_url_fopen</font> and Turn off global variables.</p>
<p>i) Disable <font face='courier new'>allow_url_fopen</font><br />
- If <font face='courier new'>allow_url_fopen</font> option is turned on in the php.ini file settings, you&#8217;re basically allowing the files to be accessed via a remote location using URL, instead of local path. Attackers may input some malicious code into this function if your PHP application lacks of data filtering. In case, you really need to allow users access to the files (perhaps, text files), you are advised to use <a href="http://php.net/manual/en/book.curl.php">Client URL (cURL)</a> functions instead of using <font face='courier new'>allow_url_fopen</font>. So, make sure this option is always set as &#8216;off&#8217; mode in php.ini file.</p>
<p>ii) Turning off global variables<br />
- Most of the time, those novice programmers (who lack of experience) preferred to use this advantage by turning on the global variables in php.ini file. In fact, by turning on this option, a developer basically provides the way to attackers to overwrite the values that have been assigned to <font face='courier new'>$_GET</font>, <font face='courier new'>$_POST</font> request. This feature is disabled by default from PHP 4.2 onwards and it’s been deprecated as of PHP 5.3.0 and completely removed as of PHP 6.0. So, turning on this option is highly discouraged.</p>
<p>@4: Avoid JavaScript&#8217;s <font face='courier new'>eval()</font><br />
<font face='courier new'>eval()</font> is a built in function in JavaScript and used for evaluating strings and executing the particular string as JavaScript code. It&#8217;s convenient and from time to time developers/front-end developers will be using it to fulfill certain needs. However, if possible, try to avoid it. As far as I&#8217;m concerned, many developers are using this function for converting JSON data into valid JavaScript objects, a better term &#8211; parsing JSON data. Frankly, I&#8217;m one of them for the sake of convenience and its simplicity. Consider this simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> jsonData <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>A line of code shown as above is the simplest method for parsing JSON data, most of the time that sent from the back-end to front-end. Or better manner as such:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> JSON_object <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>
jsonData .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&quot;(\\.|[^&quot;\\])*&quot;/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> jsonData <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>You may get the expected result by running the code snippet above. However, somehow, you are actually providing vulnerabilities to attackers to inject malicious code to eval() function, especially when you&#8217;re retrieving JSON data from unknown source. </p>
<p>The more secure and effective method used for parsing JSON data is <a href="http://www.json.org/js.html">Crockford&#8217;s JSON parser</a>. In those modern browsers with most recent version, such as Firefox 3.5 and IE 8.0 have actually provided the built in JSON parser. So, you may use the JSON parser for converting JSON data, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"> <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>jsonData<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Alternatively, you may also download the utility from <a href="http://www.json.org/js.html">http://www.json.org/js.html</a> for the support in prior version of browsers.</p>
<p>Security topic is not enough and well explained in just a single post. The above points I highlighted are just tip of the iceberg. However, keep in mind that, some good practices should always be followed when developing your own web application/website, in order to reduce security vulnerabilities. </p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2009/11/some-security-practices-in-web-development-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript &#8211; Testing on &#8216;Prototype&#8217; Together with Object Literal</title>
		<link>http://borisding.com/2009/11/javascript-testing-on-prototype-together-with-object-literal/</link>
		<comments>http://borisding.com/2009/11/javascript-testing-on-prototype-together-with-object-literal/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 04:42:13 +0000</pubDate>
		<dc:creator>boris</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://borisding.com/?p=1168</guid>
		<description><![CDATA[It&#8217;s exciting whenever I&#8217;m playing around with JavaScript. Today, I suddenly have this question come across to my mind; how do I use prototype method together with object literal? Is there any way to do something different with the traditional method? Ok, here is what I have tested out:
Saying that, I have a function which [...]


Related posts:<ol><li><a href='http://borisding.com/2009/07/javascript-different-methods-of-writing-hello-world/' rel='bookmark' title='Permanent Link: JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;'>JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;</a></li><li><a href='http://borisding.com/2009/12/advanced-js-having-fun-with-javascript-design-patterns/' rel='bookmark' title='Permanent Link: Advanced JS: Having Fun with JavaScript Design Patterns'>Advanced JS: Having Fun with JavaScript Design Patterns</a></li><li><a href='http://borisding.com/2009/01/176/' rel='bookmark' title='Permanent Link: JavaScript &#8211; The Object-oriented Programming'>JavaScript &#8211; The Object-oriented Programming</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s exciting whenever I&#8217;m playing around with JavaScript. Today, I suddenly have this question come across to my mind; how do I use prototype method together with object literal? Is there any way to do something different with the traditional method? Ok, here is what I have tested out:</p>
<p>Saying that, I have a function which will be playing as constructor by having one argument with it, and I need to create another function that will display my name, from the property that resides in the constructor. Ok, the typical method we do could be something like so:</p>
<p>Traditional prototype in Object-oriented:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> myFnc<span style="color: #009900;">&#40;</span> myName <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> myName;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    myFnc.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> myFnc<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Boris Ding&quot;</span> <span style="color: #009900;">&#41;</span>;
    obj.<span style="color: #660066;">displayName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Well, as expected, I was able to get the result I want. Then, I have tried out and rewritten the functions above in object-literal pattern, as follows:</p>
<p>Prototype with Object-literal:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">     <span style="color: #003366; font-weight: bold;">var</span> myObj <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        doSomething <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            myFnc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> myName <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> myName;
            <span style="color: #009900;">&#125;</span>            
            <span style="color: #009966; font-style: italic;">/*some block of JS code below*/</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> doSomething;
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
     myObj.<span style="color: #660066;">myFnc</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #009900;">&#41;</span>;       
     <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Interestingly, everything just works pretty well. I executed the code in the most recent version of Firefox, Safari, IE, Chrome and Opera browsers, it works perfectly. I will assume this should be working fine in prior version of those mentioned browsers, as well. (Or, you may try it out?)</p>
<p>The assumption I made is, since we&#8217;re able to call the particular function that resides in the curly brackets {}, via the object, for instance to access &#8216;myFnc&#8217; function, it can be invoked like this: <font face="courier new">myObj.myFnc()</font>, yet,  it’s playing as constructor at the same time, so I just extend the function with the prototype that is shown as above, in order to display the name from the constructor&#8217;s property; here how it looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">     myObj.<span style="color: #660066;">myFnc</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #009900;">&#41;</span>;       
     <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, as mentioned, it proved that this method is working pretty well together with object-literal. Perhaps, it will make the code clearer and well-organized in somewhere, even though the code does look a bit bulky in this manner. What do you think? =)</p>


<p>Related posts:<ol><li><a href='http://borisding.com/2009/07/javascript-different-methods-of-writing-hello-world/' rel='bookmark' title='Permanent Link: JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;'>JavaScript: Different Methods of Writing &#8220;Hello World!&#8221;</a></li><li><a href='http://borisding.com/2009/12/advanced-js-having-fun-with-javascript-design-patterns/' rel='bookmark' title='Permanent Link: Advanced JS: Having Fun with JavaScript Design Patterns'>Advanced JS: Having Fun with JavaScript Design Patterns</a></li><li><a href='http://borisding.com/2009/01/176/' rel='bookmark' title='Permanent Link: JavaScript &#8211; The Object-oriented Programming'>JavaScript &#8211; The Object-oriented Programming</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://borisding.com/2009/11/javascript-testing-on-prototype-together-with-object-literal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
