<?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>MyFunnyDev</title>
	<atom:link href="http://michalkuklis.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://michalkuklis.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 17:45:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>inspiring talks</title>
		<link>http://michalkuklis.com/blog/2012/01/14/inspiring-talks/</link>
		<comments>http://michalkuklis.com/blog/2012/01/14/inspiring-talks/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 17:45:50 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=487</guid>
		<description><![CDATA[Here is the list of talks &#38; presentations which inspired me in some ways: 1. Steve Jobs&#8217; 2005 Stanford Commencement Address 2. David Heinemeier Hansson at Startup School 08 3. Web 2.0 Expo NY: Gary Vaynerchuk Please let me know if you run into any interesting talks I will keep adding them here.]]></description>
			<content:encoded><![CDATA[<p>Here is the list of talks &amp; presentations which inspired me in some ways:</p>
<p>1. Steve Jobs&#8217; 2005 Stanford Commencement Address</p>
<p><iframe width="500" height="375" src="http://www.youtube.com/embed/UF8uR6Z6KLc?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>2. David Heinemeier Hansson at Startup School 08</p>
<p><iframe width="500" height="375" src="http://www.youtube.com/embed/0CDXJ6bMkMY?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>3. Web 2.0 Expo NY: Gary Vaynerchuk</p>
<p><iframe width="500" height="375" src="http://www.youtube.com/embed/EhqZ0RU95d4?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Please let me know if you run into any interesting talks I will keep adding them here.</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2012/01/14/inspiring-talks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Node.js libs for realtime web apps</title>
		<link>http://michalkuklis.com/blog/2011/09/26/node-js-libs-for-realtime-web-apps/</link>
		<comments>http://michalkuklis.com/blog/2011/09/26/node-js-libs-for-realtime-web-apps/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 02:13:49 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=481</guid>
		<description><![CDATA[Looks like the number of frameworks/libs/tools available for &#8220;realtime&#8221; web apps is growing. I&#8217;ve been following them since socket.io appeared for the first time. Below is the list of tools I ran into in the past. I will try to keep this list up to date. socket.io nowjs faye juggernaut]]></description>
			<content:encoded><![CDATA[<p>Looks like the number of frameworks/libs/tools available for &#8220;realtime&#8221; web apps is growing. I&#8217;ve been following them since <a href="http://socket.io">socket.io</a> appeared for the first time. Below is the list of tools I ran into in the past. I will try to keep this list up to date.</p>
<ol>
<li><a href="http://socket.io">socket.io</a></li>
<li><a href="http://nowjs.com">nowjs</a></li>
<li><a href="http://faye.jcoglan.com">faye</a></li>
<li><a href="https://github.com/maccman/juggernaut">juggernaut</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/09/26/node-js-libs-for-realtime-web-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Jasmine Matchers</title>
		<link>http://michalkuklis.com/blog/2011/07/03/extending-jasmine-matchers/</link>
		<comments>http://michalkuklis.com/blog/2011/07/03/extending-jasmine-matchers/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 04:27:04 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=468</guid>
		<description><![CDATA[Jasmine is a behavior-driven development framework for testing JavaScript from Pivotal Labs (I believe Rajan Agaskar was/is the original author). I&#8217;ve been using Jasmine for a while now to test my JavaScript code. Today I ran into interesting situation where I wanted to test events bound to jQuery element. Basically by writing code like this: [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://pivotal.github.com/jasmine/">Jasmine</a> is a behavior-driven development framework for testing JavaScript from <a href="http://pivotallabs.com/">Pivotal Labs </a> (I believe <a href="http://agaskar.com/">Rajan Agaskar</a> was/is the original author). I&#8217;ve been using Jasmine for a while now to test my JavaScript code. Today I ran into interesting situation where I wanted to test events bound to jQuery element. Basically by writing code like this:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#el'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// handler code goes here</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>I wanted to know if element <strong>$(&#8216;#el&#8217;)</strong> had any events attached to it. I looked around but couldn&#8217;t find any predefined matchers in jasmine or <a href="https://github.com/velesin/jasmine-jquery">jasmine-jquery</a> to accomplish it. I realized that it&#8217;s pretty easy to extend jasmine and define my own matchers (which is pretty cool).</p>
<p>Here is how you can do it:</p>
<p>First define your new matchers:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> eventMatchers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; toHaveEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">actual</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>eventName <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#91;</span>obj<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">events</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span>obj<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">events</span>.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>eventName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event <span style="color: #339933;">===</span> eventName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Then in your spec add your handlers inside beforeEach:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Element&quot;</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><br />
&nbsp; beforeEach<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><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addMatchers</span><span style="color: #009900;">&#40;</span>eventMatchers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>That&#8217;s it! Now you can use your new matchers inside your spec. In this case I was able to use it like this:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Element&quot;</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><br />
&nbsp; beforeEach<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><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addMatchers</span><span style="color: #009900;">&#40;</span>eventMatchers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;when event attached&quot;</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><br />
&nbsp; &nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;should contain attached event&quot;</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><br />
&nbsp; &nbsp; &nbsp; setFixtures<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;el&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#el'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#el'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toHaveEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/07/03/extending-jasmine-matchers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ssh without a hostname and password</title>
		<link>http://michalkuklis.com/blog/2011/06/15/ssh-without-host-and-password/</link>
		<comments>http://michalkuklis.com/blog/2011/06/15/ssh-without-host-and-password/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 20:15:37 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=431</guid>
		<description><![CDATA[Often I have to login to different machines. It&#8217;s annoying to always type the full hostname and then password. So here is what you can do to avoid typing too much: 1. On your local machine create a private and public key: ssh-keygen -t rsa by default 2 keys will be created (id_rsa, id_rsa.pub) under [...]]]></description>
			<content:encoded><![CDATA[<p>Often I have to login to different machines. It&#8217;s annoying to always type the full hostname and then password. So here is what you can do to avoid typing too much:</p>
<p>1. On your local machine create a private and public key:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa</div></div>
<p>by default 2 keys will be created (<strong>id_rsa, id_rsa.pub</strong>) under <strong>~/.ssh</strong></p>
<p>2. Create <strong>config</strong> file under <strong>~/.ssh</strong></p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">touch</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>config</div></div>
<p>3. Add your connection information to config file:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Host HOST_ALIAS <span style="color: #666666; font-style: italic;"># can be anything you want</span><br />
Hostname HOST_NAME <span style="color: #666666; font-style: italic;"># example.com</span><br />
User USERNAME<br />
port PORT</div></div>
<p>4. Create .ssh folder on the remote machine from your local machine (you will be prompted for the password):</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ssh</span> HOST_ALIAS <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> .ssh</div></div>
<p>5. Finally append your new public key <strong>id_rsa.pub</strong> to <strong>~/.ssh/authorized_keys</strong> on your remote machine (you will be prompted for the password one last time):</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">ssh</span> HOST_ALIAS <span style="color: #ff0000;">'cat &gt;&gt; .ssh/authorized_keys'</span></div></div>
<p>That&#8217;s all! Now every time you want to login to your remote machine you can type:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ssh</span> HOST_ALIAS</div></div>
<p>and you should be able to get in.</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/06/15/ssh-without-host-and-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>debounce in JavaScript</title>
		<link>http://michalkuklis.com/blog/2011/06/04/debounce-in-javascript/</link>
		<comments>http://michalkuklis.com/blog/2011/06/04/debounce-in-javascript/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 03:43:02 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=422</guid>
		<description><![CDATA[I ran into a situation recently where a function would get executed repeatedly after given timeout. I wanted to make sure that only last execution was taken into the consideration and I came up with: function debounce&#40;fn, wait&#41; &#123; &#160; var timeout = null; &#160; return function &#40;&#41; &#123; &#160; &#160; clearTimeout&#40;timeout&#41;; &#160; &#160; var [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a situation recently where a function would get executed repeatedly after given timeout. I wanted to make sure that only last execution was taken into the consideration and I came up with:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> debounce<span style="color: #009900;">&#40;</span>fn<span style="color: #339933;">,</span> wait<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</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><br />
&nbsp; &nbsp; clearTimeout<span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> arguments<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; timeout <span style="color: #339933;">=</span> setTimeout<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><br />
&nbsp; &nbsp; &nbsp; fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> wait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>now I can use it like this:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> printD <span style="color: #339933;">=</span> debounce<span style="color: #009900;">&#40;</span><span style="color: #000066;">print</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
printD<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
printD<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
printD<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>With this approach only last call will get executed with the value <strong>3</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/06/04/debounce-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>prototype, [[Prototype]], __proto__, constructor in JavaScript</title>
		<link>http://michalkuklis.com/blog/2011/05/07/prototype-prototype-__proto__-constructor-in-javascript/</link>
		<comments>http://michalkuklis.com/blog/2011/05/07/prototype-prototype-__proto__-constructor-in-javascript/#comments</comments>
		<pubDate>Sun, 08 May 2011 02:04:20 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=370</guid>
		<description><![CDATA[prototype, [[Prototype]], __proto__, constructor can be confusing. Here are a few notes which I&#8217;m writting mostly for myself but maybe it will help somebody else: prototype from the Function point of view: every function has access to a publically available prototype property: function Foo&#40;&#41; &#123;&#125; console.log&#40;Foo.prototype&#41;; //Object the value of the prototype property references a [...]]]></description>
			<content:encoded><![CDATA[<p>prototype, [[Prototype]], __proto__, constructor can be confusing. Here are a few notes which I&#8217;m writting mostly for myself but maybe it will help somebody else:</p>
<p><strong>prototype from the Function point of view:</strong></p>
<ul>
<li>every function has access to a publically available prototype property:</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Object</span></div></div>
<ul>
<li>the value of the prototype property references a prototype object</li>
<li>the prototype object contains an attribute called constructor. The value of the constructor attribute references the function itself when the function is orginally created.</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constuctor</span> <span style="color: #339933;">===</span> Foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<p><strong>prototype from the object point of view:</strong></p>
<ul>
<li>every object has access to prototype object</li>
<li>the prototype is not publicly accessible from the object. The object has an <strong>implicit</strong> access to the prototype via internal <strong>[[Prototype]]</strong> property</li>
<li>when object is created the value of the [[Prototype]] <strong> implicitly </strong> references the same object as prototype property of the constructor function</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// pseudo code</span><br />
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
foo.<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>Prototype<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Prototype <span style="color: #339933;">&lt;=</span> Foo.<span style="color: #660066;">prototype</span></div></div>
<ul>
<li>some implementations expose a public attribute on objects called <strong>__proto__</strong> which can be used to access prototype object</li>
<p> (<strong>You should never relay on __proto__ since it&#8217;s not a standard. I&#8217;m using it here for demo purposes.)</strong>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.__proto__ <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<ul>
<li>it&#8217;s important to mention that if the value of the prototype attribute attached to the function changes, the object created before the change happens will still point to the old Prototype object.</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Foo.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.__proto__ <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// false</span></div></div>
<p>Here are few more examples which illustrate it:<br />
<script src="https://gist.github.com/959363.js?file=gistfile1.js"></script></p>
<p>Please check this exceptional <a href="http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/">article</a> written by Dmitry A. Soshnikov if you want to learn more about the topic.</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/05/07/prototype-prototype-__proto__-constructor-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>object literals chaining via prototype in JavaScript</title>
		<link>http://michalkuklis.com/blog/2011/02/27/object-literals-chaining-via-prototype-in-javascript/</link>
		<comments>http://michalkuklis.com/blog/2011/02/27/object-literals-chaining-via-prototype-in-javascript/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 05:54:59 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=361</guid>
		<description><![CDATA[Recently I played with the idea of chaining object literals via prototype. Here is what I came up with: function chain&#40;&#41; &#123; &#160; var cur = null, &#160; &#160; &#160; &#160; prev = null; &#160; for &#40;var i = l = arguments.length - 1; i &#62;= 0; i--&#41; &#123; &#160; &#160; prev = cur; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I played with the idea of chaining object literals via prototype. Here is what I came up with:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> chain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> cur <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; prev <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; <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> l <span style="color: #339933;">=</span> arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; prev <span style="color: #339933;">=</span> cur<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; cur <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>params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> param <span style="color: #000066; font-weight: bold;">in</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>param<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> params<span style="color: #009900;">&#91;</span>param<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; cur.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> prev<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> cur<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>and an example usage:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>a<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>b<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> c <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>c<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<span style="color: #003366; font-weight: bold;">var</span> chained <span style="color: #339933;">=</span> chain<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>chained.<span style="color: #660066;">c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 3</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/02/27/object-literals-chaining-via-prototype-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating new objects in JavaScript</title>
		<link>http://michalkuklis.com/blog/2011/01/12/javascript-creating-new-objects/</link>
		<comments>http://michalkuklis.com/blog/2011/01/12/javascript-creating-new-objects/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 07:35:26 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=318</guid>
		<description><![CDATA[I&#8217;m writing this to remind myself about what happens when new object is constructed in JavaScript. This is based on the knowledge I have so far. Please let me know if you find something which is not correct. Every object in JavaScript has a property called [[Prototype]] which is hidden and not accessible. The exceptions [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing this to remind myself about what happens when new object is constructed in JavaScript. This is based on the knowledge I have so far. Please let me know if you find something which is not correct.</p>
<p>Every object in JavaScript has a property called <strong>[[Prototype]]</strong> which is hidden and not accessible. The exceptions are functions which are also objects in JavaScript. Each function has an additional public property called <strong>prototype</strong>.</p>
<p>This property is used when setting up a prototypal inheritance. It&#8217;s important to mention that public prototype property is not equal to the private hidden <strong>[[Prototype]]</strong> property.</p>
<p>In order to create a new object of the specific type the constructor has to be defined first. In JavaScript constructors are just regular functions. For example:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// function which will be used to construct new object (constructor)</span><br />
<span style="color: #003366; font-weight: bold;">function</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></div>
<p>A new object can be created with the operator <strong>&#8216;new&#8217;</strong>:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// new object is created</span><br />
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #339933;">;</span></div></div>
<p>The operator <strong>&#8216;new&#8217;</strong> in the line above does a few things: </p>
<p>1) it creates a new object with the <strong>[[Prototype]]</strong> property<br />
2) the <strong>[[Prototype]]</strong> property of the newly created object is set to the prototype of the function used as a constructor to create the object. This sounds complicated but it&#8217;s actually pretty simple. Let&#8217;s take a look at this code:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// __proto__ is a visible &nbsp;</span><br />
<span style="color: #006600; font-style: italic;">// in some browsers (Chrome, Safari, FF)</span><br />
<span style="color: #006600; font-style: italic;">// and it's deprecated now: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/proto</span><br />
<span style="color: #006600; font-style: italic;">// the [[Prototype]] property is set to the prototype of the constructor (function)</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.__proto__ <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<p>3) The newly created object is passed to Foo() as the implicit parameter <strong>&#8216;this&#8217;</strong><br />
4) The object <strong>&#8216;this&#8217;</strong> is returned and assigned to the variable <strong>foo</strong></p>
<p>Foo&#8217;s public prototype property is an object itself and has access to it&#8217;s own [[Prototype]] property. On top of that it also has an extra property called constructor which points back to function Foo.</p>
<p>We can test it with:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Foo<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<p>Thanks to the prototypal nature of JavaScript we can access the properties of the Foo&#8217;s prototype directly from foo. Since we alredy know that:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.__proto__ <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<p>and:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// Foo</span></div></div>
<p>based on the above we know that:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span></div></div>
<p>What actually happens when we try to access constructor property from foo is that first the runtime looks up the property on the object foo level and it doesn&#8217;t find it:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'constructor'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// false</span></div></div>
<p>then it looks up the property in the object referenced by [[Prototype]] property.<br />
Since in this case we know that the [[Prototype]] of foo points to the public prototype property on Foo, the constructor is present and visible.</p>
<p>Here are the tests together:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #339933;">;</span> <br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Foo<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.__proto__ <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// Foo</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'constructor'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// false</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2011/01/12/javascript-creating-new-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rendering generic views in rails 3</title>
		<link>http://michalkuklis.com/blog/2010/09/27/rendering-generic-views-rails-3/</link>
		<comments>http://michalkuklis.com/blog/2010/09/27/rendering-generic-views-rails-3/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 12:40:16 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=313</guid>
		<description><![CDATA[# serve mustache templates class TemplatesController &#60; ActionController::Metal &#160; # include render functionality &#160; include ActionController::Rendering &#160; append_view_path Rails.root.join&#40;&#34;app&#34;, &#34;views&#34;&#41; &#160; def serve &#160; &#160; if env&#91;&#34;PATH_INFO&#34;&#93; =~ /^\/templates\/&#40;.+&#41;$/ &#160; &#160; &#160; key = $1 &#160; &#160; &#160; &#160; &#160; &#160; begin &#160; &#160; &#160; &#160; render :file =&#62; &#34;templates/properties/_#{key}.html.mustache&#34; &#160; &#160; &#160; rescue &#160; [...]]]></description>
			<content:encoded><![CDATA[<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:700px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># serve mustache templates</span><br />
<span style="color:#9966CC; font-weight:bold;">class</span> TemplatesController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Metal</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># include render functionality</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Rendering</span><br />
<br />
&nbsp; append_view_path Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;app&quot;</span>, <span style="color:#996600;">&quot;views&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> serve<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;PATH_INFO&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^\<span style="color:#006600; font-weight:bold;">/</span>templates\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span><br />
&nbsp; &nbsp; &nbsp; key = $1 &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; render <span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;templates/properties/_#{key}.html.mustache&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">rescue</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">status</span> = <span style="color:#ff3333; font-weight:bold;">:file_not_found</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">content_type</span> = <span style="color:#996600;">'text/plain'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">response_body</span> = <span style="color:#996600;">''</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">status</span> = <span style="color:#ff3333; font-weight:bold;">:file_not_found</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">content_type</span> = <span style="color:#996600;">'text/plain'</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">response_body</span> = <span style="color:#996600;">''</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2010/09/27/rendering-generic-views-rails-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript closures</title>
		<link>http://michalkuklis.com/blog/2010/06/25/javascript-closures-2/</link>
		<comments>http://michalkuklis.com/blog/2010/06/25/javascript-closures-2/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 05:37:54 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=302</guid>
		<description><![CDATA[For given content: &#160; &#60;div id=&#34;test0&#34;&#62;change me&#60;/div&#62; &#160; &#60;div id=&#34;test1&#34;&#62;change me&#60;/div&#62; &#160; &#60;div id=&#34;test2&#34;&#62;change me&#60;/div&#62; &#160; &#60;div id=&#34;test3&#34;&#62;change me&#60;/div&#62; The code: &#160;for &#40;var i = 0; i &#60; 4; i++&#41; &#123; &#160; &#160;$&#40;'#test' + i&#41;.click&#40;function&#40;&#41;&#123; &#160; &#160; &#160;$&#40;this&#41;.html&#40;i&#41;; &#160; &#160;&#125;&#41;; &#125; will print (every time any of the elements is clicked): 4 4 4 4 [...]]]></description>
			<content:encoded><![CDATA[<p>For given content:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;test0&quot;</span><span style="color: #339933;">&gt;</span>change me<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;test1&quot;</span><span style="color: #339933;">&gt;</span>change me<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;test2&quot;</span><span style="color: #339933;">&gt;</span>change me<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;test3&quot;</span><span style="color: #339933;">&gt;</span>change me<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></div></div>
<p>The code:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<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> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#test'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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><br />
&nbsp; &nbsp; &nbsp;$<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>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>will print (every time any of the elements is clicked):</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #CC0000;">4</span><br />
<span style="color: #CC0000;">4</span><br />
<span style="color: #CC0000;">4</span><br />
<span style="color: #CC0000;">4</span></div></div>
<p>Here is how this could be solved with the closure:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <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> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#test'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<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>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2010/06/25/javascript-closures-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

