<?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 &#187; JavaScript</title>
	<atom:link href="http://michalkuklis.com/blog/tag/javascript/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>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>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>
		<item>
		<title>JQuery 1.3</title>
		<link>http://michalkuklis.com/blog/2009/01/15/jquery-13/</link>
		<comments>http://michalkuklis.com/blog/2009/01/15/jquery-13/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 03:51:00 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=112</guid>
		<description><![CDATA[New release contains new selector engine called Sizzle (the fastest css selector out there) and live events (which work similar to the live query plugin). More details can be found here]]></description>
			<content:encoded><![CDATA[<p>New release contains new selector engine called Sizzle (the fastest css selector out there) and  live events (which work similar to the <a href="http://michalkuklis.com/blog/?p=30">live query plugin</a>). </p>
<p>More details can be found <a href="http://docs.jquery.com/Release:jQuery_1.3">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2009/01/15/jquery-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peppy fast css3 selector engine</title>
		<link>http://michalkuklis.com/blog/2008/10/28/peppy-fast-css3-selector-engine/</link>
		<comments>http://michalkuklis.com/blog/2008/10/28/peppy-fast-css3-selector-engine/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 02:41:48 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=68</guid>
		<description><![CDATA[Peppy is a small and very fast css3 selector written by James Donaghue. Here is how you can use it: var selector = &#34;div&#34;; var context = &#34;#elementId&#34;; var q = peppy.query&#40;selector, context&#41;;]]></description>
			<content:encoded><![CDATA[<p><a href="http://jamesdonaghue.com/static/peppy/">Peppy </a> is a small and very fast css3 selector written by James Donaghue. Here is how you can use it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span class="kw2">var</span> selector <span class="sy0">=</span> <span class="st0">&quot;div&quot;</span><span class="sy0">;</span>
<span class="kw2">var</span> context <span class="sy0">=</span> <span class="st0">&quot;#elementId&quot;</span><span class="sy0">;</span>
<span class="kw2">var</span> q <span class="sy0">=</span> peppy.<span class="me1">query</span><span class="br0">&#40;</span>selector<span class="sy0">,</span> context<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2008/10/28/peppy-fast-css3-selector-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript closures</title>
		<link>http://michalkuklis.com/blog/2008/09/16/javascript-closures/</link>
		<comments>http://michalkuklis.com/blog/2008/09/16/javascript-closures/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 02:30:25 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=38</guid>
		<description><![CDATA[Nice presentation about JavaScript closures posted by Stuart Langridge]]></description>
			<content:encoded><![CDATA[<p>Nice presentation about <a title="JavaScript closures" href="http://www.kryogenix.org/code/browser/secrets-of-javascript-closures/">JavaScript closures</a> posted by Stuart Langridge</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2008/09/16/javascript-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

