<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How to create an object with private variables and methods</title>
	<atom:link href="http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: boiledwater</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-839</link>
		<dc:creator>boiledwater</dc:creator>
		<pubDate>Thu, 29 Oct 2009 08:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-839</guid>
		<description>yes,the essential is closure!</description>
		<content:encoded><![CDATA[<p>yes,the essential is closure!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to write a singleton class in javascript &#124; Javascript Kata</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-805</link>
		<dc:creator>How to write a singleton class in javascript &#124; Javascript Kata</dc:creator>
		<pubDate>Mon, 05 Oct 2009 14:49:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-805</guid>
		<description>[...] the new solution using a private variable for the [...]</description>
		<content:encoded><![CDATA[<p>[...] the new solution using a private variable for the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-791</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 25 Sep 2009 21:01:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-791</guid>
		<description>@BK You could do it that way to. I just prefer the other notation... thanks for commenting about this way of doing things. 

Keep them coming, I&#039;ll probably write a post about how many different ways of using private variables in an object ;)</description>
		<content:encoded><![CDATA[<p>@BK You could do it that way to. I just prefer the other notation&#8230; thanks for commenting about this way of doing things. </p>
<p>Keep them coming, I&#8217;ll probably write a post about how many different ways of using private variables in an object <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-790</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:36:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-790</guid>
		<description>Forgot to remove the &quot;return&quot; line.

Here is a simpler example:

function test(){
	var rot=&quot;a&quot;;
	this.rotted=function(){return rot};
}

var test2 = new test()
alert(test2.rot); //Returns undefined
alert(test2.rotted()); //Returns a</description>
		<content:encoded><![CDATA[<p>Forgot to remove the &#8220;return&#8221; line.</p>
<p>Here is a simpler example:</p>
<p>function test(){<br />
	var rot=&#8221;a&#8221;;<br />
	this.rotted=function(){return rot};<br />
}</p>
<p>var test2 = new test()<br />
alert(test2.rot); //Returns undefined<br />
alert(test2.rotted()); //Returns a</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-789</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:35:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-789</guid>
		<description>Oups... Forgot to remove the return line :P

Simpler sample:

function test(){
	var rot=&quot;a&quot;;
	this.rotted=function(){return rot};
}
var test2 = new test()
alert(test2.rot); //returns undefined
alert(test2.rotted()); //returns a</description>
		<content:encoded><![CDATA[<p>Oups&#8230; Forgot to remove the return line <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Simpler sample:</p>
<p>function test(){<br />
	var rot=&#8221;a&#8221;;<br />
	this.rotted=function(){return rot};<br />
}<br />
var test2 = new test()<br />
alert(test2.rot); //returns undefined<br />
alert(test2.rotted()); //returns a</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-788</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:33:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-788</guid>
		<description>Scoping variables is one of the base OO principle: encapsulation. This is the main reason I think to do it.

Also, why not simply do

function PrivateCats() {
  // This is the list of cat names
  var nameList = [];
  
  // This is a private method
  var lastCat = function() {
    // Note : I don’t use &quot;this&quot; to access private variables
    // thanks to the power of closures!
    return nameList[nameList.length-1];
  }
  
  // These are our public methods!
  // This is where we create another scope to
  // avoid external objects to use the private variables.
  this.add = function(name) {
      // Note : once again, I don’t use &quot;this&quot; 
      // to access the private variables and methods
      nameList.push(name);
      return lastCat();
    },
    this.names = function() {
      return nameList;
    }
}

Seems even more simple I think... And works the same since this isn&#039;t in the same scope as the var declarations. Maybe I missed something... I&#039;m starting to become rusted in JS :P</description>
		<content:encoded><![CDATA[<p>Scoping variables is one of the base OO principle: encapsulation. This is the main reason I think to do it.</p>
<p>Also, why not simply do</p>
<p>function PrivateCats() {<br />
  // This is the list of cat names<br />
  var nameList = [];</p>
<p>  // This is a private method<br />
  var lastCat = function() {<br />
    // Note : I don’t use &#8220;this&#8221; to access private variables<br />
    // thanks to the power of closures!<br />
    return nameList[nameList.length-1];<br />
  }</p>
<p>  // These are our public methods!<br />
  // This is where we create another scope to<br />
  // avoid external objects to use the private variables.<br />
  this.add = function(name) {<br />
      // Note : once again, I don’t use &#8220;this&#8221;<br />
      // to access the private variables and methods<br />
      nameList.push(name);<br />
      return lastCat();<br />
    },<br />
    this.names = function() {<br />
      return nameList;<br />
    }<br />
}</p>
<p>Seems even more simple I think&#8230; And works the same since this isn&#8217;t in the same scope as the var declarations. Maybe I missed something&#8230; I&#8217;m starting to become rusted in JS <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-787</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Thu, 24 Sep 2009 14:13:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-787</guid>
		<description>@Yansky It&#039;s less about collisions and more about cleaner code. If all the methods of an object are public, it could be a harder for other programmer to use your code.</description>
		<content:encoded><![CDATA[<p>@Yansky It&#8217;s less about collisions and more about cleaner code. If all the methods of an object are public, it could be a harder for other programmer to use your code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yansky</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-786</link>
		<dc:creator>Yansky</dc:creator>
		<pubDate>Thu, 24 Sep 2009 09:26:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-786</guid>
		<description>I don&#039;t see the advantage to using private variables as opposed to namespacing. Sure it may be accessible outside of the function, but the fact that it&#039;s namespaced means there wont be any problems with variable name collisions right?
e.g.

function foo(){
foo.bar={}; 
}

var bar={}; //no collision</description>
		<content:encoded><![CDATA[<p>I don&#8217;t see the advantage to using private variables as opposed to namespacing. Sure it may be accessible outside of the function, but the fact that it&#8217;s namespaced means there wont be any problems with variable name collisions right?<br />
e.g.</p>
<p>function foo(){<br />
foo.bar={};<br />
}</p>
<p>var bar={}; //no collision</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-785</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Wed, 23 Sep 2009 18:15:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-785</guid>
		<description>@Topper There&#039;s a hundred ways of assigning functions and I picked the easiest one to understand but it may be hard to read when the class grows. Sometimes, I use _privateMethod() to distinguish them easily from public methods but I&#039;m not decided if I should really use this notation.

Anyways, thanks for your thoughts! Someone that would read the comments would see other ways to do it.</description>
		<content:encoded><![CDATA[<p>@Topper There&#8217;s a hundred ways of assigning functions and I picked the easiest one to understand but it may be hard to read when the class grows. Sometimes, I use _privateMethod() to distinguish them easily from public methods but I&#8217;m not decided if I should really use this notation.</p>
<p>Anyways, thanks for your thoughts! Someone that would read the comments would see other ways to do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Topper</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-784</link>
		<dc:creator>Topper</dc:creator>
		<pubDate>Wed, 23 Sep 2009 16:07:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-784</guid>
		<description>The only thing about specifying your scope in the return object, is that it can be confusing to read (or see where public vs private stops... this is totally just a semantic quibble but:

function PrivateCats() {
  var klass = {};

  var privateFunction = function () {};

   klass.publicFunction = function () {};

  return klass;
}

Seems to be a little easier to read.</description>
		<content:encoded><![CDATA[<p>The only thing about specifying your scope in the return object, is that it can be confusing to read (or see where public vs private stops&#8230; this is totally just a semantic quibble but:</p>
<p>function PrivateCats() {<br />
  var klass = {};</p>
<p>  var privateFunction = function () {};</p>
<p>   klass.publicFunction = function () {};</p>
<p>  return klass;<br />
}</p>
<p>Seems to be a little easier to read.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
