<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Javascript on locrian code</title>
    <link>http://blog.locrian.uk/tags/javascript/</link>
    <description>Recent content in Javascript on locrian code</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 21 Dec 2010 00:00:00 +0000</lastBuildDate>
    <atom:link href="http://blog.locrian.uk/tags/javascript/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Closure Exposure</title>
      <link>http://blog.locrian.uk/post/closure-exposure/</link>
      <pubDate>Tue, 21 Dec 2010 00:00:00 +0000</pubDate>
      
      <guid>http://blog.locrian.uk/post/closure-exposure/</guid>
      <description>

&lt;p&gt;These days people are becoming more aware that you can write real object oriented code in Javascript. Even though the language doesn&amp;rsquo;t support classes with private members directly, you can achieve the same thing quite easily using closures.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt; MyClass() {
    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Private members&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; internalCounter &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;0&lt;/span&gt;;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; somePrivateFunction &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;(input) {
        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; input &lt;span style=&#34;color: #666666&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot; foo!&amp;quot;&lt;/span&gt;;
    };

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Public members&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.getCounter &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; internalCounter;
    };
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.increment &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
        internalCounter&lt;span style=&#34;color: #666666&#34;&gt;++&lt;/span&gt;;
    };
}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the first two members are effectively private, as they can only be accessed from functions declared within MyClass.&lt;/p&gt;

&lt;p&gt;This is all well and good, and makes me feel much better when I have to write Javascript, but if you&amp;rsquo;re a serious Javascript developer you&amp;rsquo;ll want to unit test your code as you go along. How are you going to test those pesky private variables and functions?&lt;/p&gt;

&lt;h3 id=&#34;dude-let-me-in&#34;&gt;Dude, let me in!&lt;/h3&gt;

&lt;p&gt;The problem is, you can&amp;rsquo;t break into a closure because it&amp;rsquo;s enforced by the language. You can try as hard as you like, but there&amp;rsquo;s no way you&amp;rsquo;re getting access to somePrivateFunction() without modifying the code of MyClass. This is a good thing - it means the private functions are truly encapsulated. But it does make testing a bit difficult.&lt;/p&gt;

&lt;p&gt;One trick I like is to add the following line to the end of the class constructor.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;.debug &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;(code) { &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000&#34;&gt;eval&lt;/span&gt;(code); };
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because of the way the eval command works, this now means that any of the private members can be extracted from within the class. For example, we can now write a unit test for somePrivateFunction. (This is a &lt;a href=&#34;http://docs.jquery.com/Qunit&#34;&gt;QUnit&lt;/a&gt; test, which is the test framework jQuery uses).&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;test(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;somePrivateFunction&amp;quot;&lt;/span&gt;, &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;function&lt;/span&gt;() {
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; my &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; MyClass();

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// extract the private function from the closure&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;var&lt;/span&gt; somePrivateFunction &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; my.debug(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;somePrivateFunction&amp;quot;&lt;/span&gt;);

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// call the function&lt;/span&gt;
    equals(somePrivateFunction(&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;), &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;hello foo!&amp;quot;&lt;/span&gt;);
});
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We have slightly polluted the original class with this extra line. If it worries you, the &amp;lsquo;debug&amp;rsquo; lines can easily be stripped out before the code goes into production.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>