<?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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-create-an-object-with-private-variables-and-methods</link>
	<description>helping you with javascript since 2007</description>
	<lastBuildDate>Tue, 31 Jan 2012 11:22:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Parminavilum</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-1356</link>
		<dc:creator>Parminavilum</dc:creator>
		<pubDate>Wed, 12 Oct 2011 21:55:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-1356</guid>
		<description>&lt;strong&gt;Websites you should visit...&lt;/strong&gt;

[...]below you’ll find the link to some sites that we think you should visit[...]…...</description>
		<content:encoded><![CDATA[<p><strong>Websites you should visit&#8230;</strong></p>
<p>[...]below you’ll find the link to some sites that we think you should visit[...]…&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dsimard</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-1071</link>
		<dc:creator>dsimard</dc:creator>
		<pubDate>Mon, 14 Feb 2011 20:50:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-1071</guid>
		<description>Yes it would. I just wanted to expose how to do &quot;private&quot; variables (in fact, variables out of scope) but someone who would want to use prototype could do it and it would be better for performance, indeed.</description>
		<content:encoded><![CDATA[<p>Yes it would. I just wanted to expose how to do &#8220;private&#8221; variables (in fact, variables out of scope) but someone who would want to use prototype could do it and it would be better for performance, indeed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Indigo0086</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/comment-page-1/#comment-1070</link>
		<dc:creator>Indigo0086</dc:creator>
		<pubDate>Mon, 14 Feb 2011 07:41:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128#comment-1070</guid>
		<description>I have a question performance wise.  Would this not create a new object with the bundle of public functions each time you instantiate an object of the class?  I know when you declare all your public functions within the prototype object of the class outside it does it once.</description>
		<content:encoded><![CDATA[<p>I have a question performance wise.  Would this not create a new object with the bundle of public functions each time you instantiate an object of the class?  I know when you declare all your public functions within the prototype object of the class outside it does it once.</p>
]]></content:encoded>
	</item>
	<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>
</channel>
</rss>

