<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.apidesign.org/skins/common/feed.css?116"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>PrivateJavascript - Revision history</title>
		<link>http://wiki.apidesign.org/index.php?title=PrivateJavascript&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.12.0rc1</generator>
		<lastBuildDate>Thu, 07 May 2026 01:27:40 GMT</lastBuildDate>
		<item>
			<title>71.141.96.194 at 07:19, 10 September 2010</title>
			<link>http://wiki.apidesign.org/index.php?title=PrivateJavascript&amp;diff=4082&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 07:19, 10 September 2010&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     $&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;(document).ready&lt;/del&gt;(function(){&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     $(function(){&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;       var api = create($(&amp;quot;a&amp;quot;), $(&amp;quot;#counter&amp;quot;));&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;       var api = create($(&amp;quot;a&amp;quot;), $(&amp;quot;#counter&amp;quot;));&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 10 Sep 2010 07:19:59 GMT</pubDate>			<dc:creator>71.141.96.194</dc:creator>			<comments>http://wiki.apidesign.org/wiki/Talk:PrivateJavascript</comments>		</item>
		<item>
			<title>JaroslavTulach: New page: Can you hide anything in a dynamic language like Javascript? If there are no access modifiers to make something ''private'' and when everyone is allowed to...</title>
			<link>http://wiki.apidesign.org/index.php?title=PrivateJavascript&amp;diff=2703&amp;oldid=prev</link>
			<description>&lt;p&gt;New page: Can you hide anything in a dynamic language like &lt;a href=&quot;/wiki/Javascript&quot; class=&quot;mw-redirect&quot; title=&quot;Javascript&quot;&gt;Javascript&lt;/a&gt;? If there are no &lt;a href=&quot;/wiki/ClarityOfAccessModifiers&quot; title=&quot;ClarityOfAccessModifiers&quot;&gt;access modifiers&lt;/a&gt; to make something ''private'' and when everyone is allowed to...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Can you hide anything in a dynamic language like [[Javascript]]? If there are no [[ClarityOfAccessModifiers|access modifiers]] to make something ''private'' and when everyone is allowed to dynamically add new fields, functions to any object or even remove them, then it is really hard to keep something private.&lt;br /&gt;
&lt;br /&gt;
Still, as a framework writer you want to separate an [[API]] from an implementation. You need to make it hard (if not impossible) for your users to cheat and bypass your [[API]]. Documenting what is ''public'' and what is not is not enough. Users resort to documentation only when completely lost. Until then they act [[cluelessness|cluelessly]] and try to make things work first and only later (if ever) think of what they did. The users need to face a wall when they try to use an implementation detail (unless you want to get stuck as me with [[APILessAPI|one mediawiki version]]). Can that be done in [[Javascript]]?&lt;br /&gt;
&lt;br /&gt;
=== Scope instead of private ===&lt;br /&gt;
&lt;br /&gt;
The ''private'' modifier is missing in [[Javascript]]. No fields can be guarded against modification. Where can one hide own state? Inside a method body! Looks like there is no way to inspect a body of a '''function''' and as functions can be nested (and see the other functions local variables) one can use following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
 &amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
   &amp;lt;meta http-equiv=&amp;quot;Content-Type&amp;quot; content=&amp;quot;text/html; charset=utf-8&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;script src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
   &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
     function create(what,counter) {&lt;br /&gt;
	var impl = {&lt;br /&gt;
        };&lt;br /&gt;
	impl.what = what;&lt;br /&gt;
	impl.cnt = 0;&lt;br /&gt;
	impl.counter = counter;&lt;br /&gt;
	        &lt;br /&gt;
        var api = {&lt;br /&gt;
	};&lt;br /&gt;
	api.toggle = function() {&lt;br /&gt;
	   if (impl.cnt++ % 2 == 0) {&lt;br /&gt;
		$(impl.what).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
	   } else {&lt;br /&gt;
		$(impl.what).show(&amp;quot;fast&amp;quot;);&lt;br /&gt;
           }	   &lt;br /&gt;
	   $(impl.counter).text(impl.cnt);&lt;br /&gt;
 	};&lt;br /&gt;
	return api;   &lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     $(document).ready(function(){&lt;br /&gt;
       var api = create($(&amp;quot;a&amp;quot;), $(&amp;quot;#counter&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
       $(&amp;quot;a&amp;quot;).click(function(event){&lt;br /&gt;
 	 api.toggle();&lt;br /&gt;
         event.preventDefault();&lt;br /&gt;
         api.toggle();&lt;br /&gt;
       });&lt;br /&gt;
     });&lt;br /&gt;
     &lt;br /&gt;
   &amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;/head&amp;gt;&lt;br /&gt;
 &amp;lt;body&amp;gt;&lt;br /&gt;
   &amp;lt;a href=&amp;quot;http://apidesign.org/&amp;quot;&amp;gt;Toggle on and off&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;Toggle count: &amp;lt;span id=&amp;quot;counter&amp;quot;&amp;gt;0&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;/body&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The [[API]] consist of one [[factory]] method (''create'') which returns an object with ''toggle'' method (there could be more method of course). This object keeps its internal state (reference to two document elements and a counter) in separate ''impl'' object which cannot be referenced by users holding just the [[API]] object. Looks like we successfully replaced missing access modifiers with a visibility scope.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Contest ===&lt;br /&gt;
&lt;br /&gt;
I know all tricks a [[Java]] [[API]] users can do, but I do not claim to be a [[Javascript]] expert. Maybe there is a way to get on hand of the internals. Thus, let's try a contest: Who ever manages to write a [[Javascript]] code to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var api = create($(&amp;quot;a&amp;quot;),$(&amp;quot;#counter&amp;quot;);&lt;br /&gt;
api.toggle();&lt;br /&gt;
// TBD: somehow clean the internal counter&lt;br /&gt;
api.toggle();&lt;br /&gt;
// now the internal counter shall be 1 and not 2&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be a winner and gets a special section in another [[API Design Tips]] podcast.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;comments/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:APIDesignPatterns]] [[Category:APIDesignPatterns:Encapsulation]]&lt;/div&gt;</description>
			<pubDate>Wed, 05 Aug 2009 04:31:22 GMT</pubDate>			<dc:creator>JaroslavTulach</dc:creator>			<comments>http://wiki.apidesign.org/wiki/Talk:PrivateJavascript</comments>		</item>
	</channel>
</rss>