<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by EK on Medium]]></title>
        <description><![CDATA[Stories by EK on Medium]]></description>
        <link>https://medium.com/@ekong2?source=rss-7aa29f71e426------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3I0HjzNQUFFC2i2W40eRlQ.jpeg</url>
            <title>Stories by EK on Medium</title>
            <link>https://medium.com/@ekong2?source=rss-7aa29f71e426------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:24:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ekong2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding function.bind]]></title>
            <link>https://medium.com/@ekong2/understanding-function-bind-3d8d7c16d0f2?source=rss-7aa29f71e426------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d8d7c16d0f2</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[EK]]></dc:creator>
            <pubDate>Sun, 05 Jul 2015 00:38:28 GMT</pubDate>
            <atom:updated>2015-07-05T00:58:33.014Z</atom:updated>
            <content:encoded><![CDATA[<p>Function binding in JavaScript can be confusing.</p><p>Say we have two dirty dogs, Titus and Kevin. We have also declared a brush function that cleans their fur and then console.logs different messages depending on whether the dog is happy or not.</p><pre>var TitusThePitbull = {name: &#39;Titus&#39;, fur: &#39;dirty&#39;, brush: brush};<br>var KevinTheMaltese = {name: &#39;Kevin&#39;, fur: &#39;dirty&#39;, brush: brush};</pre><pre>function brush(isHappy){<br>  <strong>this</strong>.fur = &#39;clean&#39;;<br>  if (isHappy) {<br>    console.log( <strong>this</strong>.name + &#39; is clean and happy!&#39; );<br>  } else {<br>    console.log( <strong>this</strong>.name + &#39; is clean but glares at you&#39; );<br>  }<br>}</pre><pre>TitusThePitbull.brush(true);  // &#39;Titus is clean and happy!&#39;<br>KevinTheMaltese.brush();  // &#39;Kevin is clean but glares at you&#39;</pre><p>Pay attention to the <strong>this</strong> keyword inside the bark function. At calltime, <strong>this</strong> gets bound to the nearest object that invoked the function containing <strong>this</strong>.</p><p>For the first brush call, <strong>this</strong> refers to TitusThePitbull.</p><p>For the second brush call, <strong>this</strong> refers to KevinTheMaltese.</p><p>Currently, this brush works for both Kevin and Titus. But what if using the same brush is not sanitary? Say you want to have two different brushes for both of them and so you give Titus a new brush.</p><pre>var titusBrush = brush.bind(TitusThePitbull);</pre><p>Enter function.bind</p><p>What this line of code does is essentially take the brush function, makes a new copy of this brush and then “slaps on” Titus’ name tag onto it. This brush will now only brush Titus. Try calling:</p><pre>titusBrush(); // &#39;Titus is clean and happy!&#39;<br>titusBrush(false); // &#39;Titus is clean but glares at you&#39;</pre><p>You’ll notice that this is now Titus’ exclusive brush.</p><p>Let’s now walk through a simpler implementation for Function.bind</p><pre>var titusBrush = brush.bind(TitusThePitbull);<br>///////////////////////////////////////////////////////////////////</pre><pre><strong>Function.prototype.bind = function(context){</strong></pre><pre>  //Get a reference to the function you wish to bind<br>  <strong>var funcYouWantToBind = this;</strong></pre><pre>  //Return a brand new function that retains a reference to the<br>  //above funcYouWantToBind and context variables<br>  <strong>return function(){</strong></pre><pre><strong>    return funcYouWantToBind.apply(context, arguments);</strong></pre><pre><strong>  };</strong></pre><pre><strong>}</strong></pre><p>What fun.apply does here in this case is it takes funcYouWantToBind’s <strong>this</strong> keyword, sets it to <strong>context </strong>and then passes the <strong>arguments</strong> variable to this modified version of funcYouWantToBind.</p><p>Why use function.bind?</p><p>In some cases you might want to specify the <strong>this </strong>keyword explicitly to avoid problems. For example, when using setTimeout/setInterval.</p><p>Other times you might want to actually preset some values every time you invoke the function. For example if you used:</p><pre>var titusAmazingBrush = brush.bind(TitusThePitbull, true);</pre><p>This new brush will always make Titus happy. It will never make him unhappy even if you call:</p><pre>titusAmazingBrush(false);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d8d7c16d0f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>