<?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: JavaScript: how to get private, privileged, public and static members (properties and methods)</title>
	<atom:link href="http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/</link>
	<description>Web development and Internet trends</description>
	<lastBuildDate>Sat, 20 Mar 2010 05:17:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-626852</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Sat, 13 Mar 2010 22:37:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-626852</guid>
		<description>Mark,

I understand what you are saying, This was mainly meant as an introduction/execrcise for people coming from other languages, although in reality there&#039;s no exact mapping.</description>
		<content:encoded><![CDATA[<p>Mark,</p>
<p>I understand what you are saying, This was mainly meant as an introduction/execrcise for people coming from other languages, although in reality there&#8217;s no exact mapping.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Willis</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-626711</link>
		<dc:creator>Mark Willis</dc:creator>
		<pubDate>Fri, 12 Mar 2010 20:28:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-626711</guid>
		<description>I don&#039;t believe static members can be emulated.  
&lt;code&gt;Kid.town = &quot;South Park&quot;;&lt;/code&gt;
does not make &lt;i&gt;town&lt;/i&gt; a member of any Kid object (created with operator new). You have to go further and also write
&lt;code&gt;Kid.prototype.town = &quot;South Park&quot;;&lt;/code&gt;
This may be as close as you can get, but Kid.town still doesn&#039;t  emulate a static variable since the individual values of town can be changed at will without effecting the others. The best you can do is to give the Kid class and all Kid objects  identical copies and hope no one makes any changes.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t believe static members can be emulated.<br />
<code>Kid.town = "South Park";</code><br />
does not make <i>town</i> a member of any Kid object (created with operator new). You have to go further and also write<br />
<code>Kid.prototype.town = "South Park";</code><br />
This may be as close as you can get, but Kid.town still doesn&#8217;t  emulate a static variable since the individual values of town can be changed at will without effecting the others. The best you can do is to give the Kid class and all Kid objects  identical copies and hope no one makes any changes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-613480</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 08 Dec 2009 22:36:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-613480</guid>
		<description>David Serrano,

Sorry for a late reply!
And thanks, I&#039;m glad that you like my articles on JavaScript!

Adam&#039;s example does indeed work and allows the desired access. The downside of it, though, is that it will affect performance. For any method of an object that you will create several instances of, you would want to have those methods on the prototype instead.</description>
		<content:encoded><![CDATA[<p>David Serrano,</p>
<p>Sorry for a late reply!<br />
And thanks, I&#8217;m glad that you like my articles on JavaScript!</p>
<p>Adam&#8217;s example does indeed work and allows the desired access. The downside of it, though, is that it will affect performance. For any method of an object that you will create several instances of, you would want to have those methods on the prototype instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Serrano</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-598692</link>
		<dc:creator>David Serrano</dc:creator>
		<pubDate>Fri, 21 Aug 2009 07:35:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-598692</guid>
		<description>Hi Robert:

I am new at Javascript (but learning hard). Your articles are really nice.

Regarding the solution posted above by Adam Silver, I cannot see anything wrong with it; he is creating &lt;em&gt;MyClass&lt;/em&gt; prototype from another (anonymous) object (thus making a copy the &lt;em&gt;getPrivate()&lt;/em&gt; function). Once he has fed &lt;em&gt;MyClass&lt;/em&gt; prototype, I cannot see why each &lt;em&gt;MyClass&lt;/em&gt; instance needs its own copy of &lt;em&gt;getPrivate()&lt;/em&gt;. On the contrary, I think all instances share the same &lt;em&gt;getPrivate()&lt;/em&gt; function; in fact, &lt;em&gt;MyClass&lt;/em&gt; constructor is an empty function, so there is no chance to redefine a new &lt;em&gt;getPrivate()&lt;/em&gt; function each time you create a new &lt;em&gt;MyClass&lt;/em&gt; instance.
It is true that all instances share the same &lt;em&gt;privateVar&lt;/em&gt; as well, but this is something (as you have posted elsewhere) that cannot be resolved by the language (outside the constructor, I mean).

So, from my point of view, Adam&#039;s approach is fully correct.

If I am wrong, explain it to me please (with some illustrative examples if you are so kind).

Thank you in advance.</description>
		<content:encoded><![CDATA[<p>Hi Robert:</p>
<p>I am new at Javascript (but learning hard). Your articles are really nice.</p>
<p>Regarding the solution posted above by Adam Silver, I cannot see anything wrong with it; he is creating <em>MyClass</em> prototype from another (anonymous) object (thus making a copy the <em>getPrivate()</em> function). Once he has fed <em>MyClass</em> prototype, I cannot see why each <em>MyClass</em> instance needs its own copy of <em>getPrivate()</em>. On the contrary, I think all instances share the same <em>getPrivate()</em> function; in fact, <em>MyClass</em> constructor is an empty function, so there is no chance to redefine a new <em>getPrivate()</em> function each time you create a new <em>MyClass</em> instance.<br />
It is true that all instances share the same <em>privateVar</em> as well, but this is something (as you have posted elsewhere) that cannot be resolved by the language (outside the constructor, I mean).</p>
<p>So, from my point of view, Adam&#8217;s approach is fully correct.</p>
<p>If I am wrong, explain it to me please (with some illustrative examples if you are so kind).</p>
<p>Thank you in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-488865</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 22 Dec 2008 19:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-488865</guid>
		<description>John,

Yeah, basically. A privileged method can access private members, whereas, in more regular languages, public methods can&#039;t really do that.</description>
		<content:encoded><![CDATA[<p>John,</p>
<p>Yeah, basically. A privileged method can access private members, whereas, in more regular languages, public methods can&#8217;t really do that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John T</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-488734</link>
		<dc:creator>John T</dc:creator>
		<pubDate>Mon, 22 Dec 2008 10:53:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-488734</guid>
		<description>Basic question really:

I can&#039;t see any difference in the syntax for a privileged method and a public method (when not using prototype) so... is a method called public/privileged based on the variables it access?

Thanks in advance

JT.</description>
		<content:encoded><![CDATA[<p>Basic question really:</p>
<p>I can&#8217;t see any difference in the syntax for a privileged method and a public method (when not using prototype) so&#8230; is a method called public/privileged based on the variables it access?</p>
<p>Thanks in advance</p>
<p>JT.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript inheritance - experimenting with syntax alternatives and private variables - Robert&#8217;s talk - Web development and Internet trends</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-466691</link>
		<dc:creator>JavaScript inheritance - experimenting with syntax alternatives and private variables - Robert&#8217;s talk - Web development and Internet trends</dc:creator>
		<pubDate>Tue, 21 Oct 2008 21:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-466691</guid>
		<description>[...] for more information about inheritance - intrigued by the concept myself, and also inspired by Andrea Giammarchi&#8217;s comment , I though I&#8217;d come up with some scary [...]</description>
		<content:encoded><![CDATA[<p>[...] for more information about inheritance &#8211; intrigued by the concept myself, and also inspired by Andrea Giammarchi&#8217;s comment , I though I&#8217;d come up with some scary [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-465124</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 17 Oct 2008 08:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-465124</guid>
		<description>Bleyder,

Glad that you liked it! :-)</description>
		<content:encoded><![CDATA[<p>Bleyder,</p>
<p>Glad that you liked it! <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bleyder</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-465082</link>
		<dc:creator>Bleyder</dc:creator>
		<pubDate>Fri, 17 Oct 2008 08:08:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-465082</guid>
		<description>Very simple and really helpfull post. I&#039;m waiting another one related with this topic.

Thanks!!</description>
		<content:encoded><![CDATA[<p>Very simple and really helpfull post. I&#8217;m waiting another one related with this topic.</p>
<p>Thanks!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-464476</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Thu, 16 Oct 2008 17:42:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-464476</guid>
		<description>Andrea,

Well, they&#039;re shared by instances, so-to-say, but not by prototype methods, and that&#039;s what Adam was aiming for.

Also, good posting the link, it&#039;s good to have in this context!</description>
		<content:encoded><![CDATA[<p>Andrea,</p>
<p>Well, they&#8217;re shared by instances, so-to-say, but not by prototype methods, and that&#8217;s what Adam was aiming for.</p>
<p>Also, good posting the link, it&#8217;s good to have in this context!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrea Giammarchi</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-464418</link>
		<dc:creator>Andrea Giammarchi</dc:creator>
		<pubDate>Thu, 16 Oct 2008 16:35:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-464418</guid>
		<description>Guys, again do not make confusion, private variables are always shared by instances since these are in the prototype, whatever scope it has. I wrote these details in my document, Robert if you do not mind I would like to post it again: http://www.3site.eu/doc/

Kind Regards :)</description>
		<content:encoded><![CDATA[<p>Guys, again do not make confusion, private variables are always shared by instances since these are in the prototype, whatever scope it has. I wrote these details in my document, Robert if you do not mind I would like to post it again: <a href="http://www.3site.eu/doc/" rel="nofollow">http://www.3site.eu/doc/</a></p>
<p>Kind Regards <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463206</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:37:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463206</guid>
		<description>Adam,

I think I will write one, in some form at least. :-)</description>
		<content:encoded><![CDATA[<p>Adam,</p>
<p>I think I will write one, in some form at least. <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Silver</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463204</link>
		<dc:creator>Adam Silver</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:36:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463204</guid>
		<description>Ah ha!  I see!  You need to do a blog post on all this stuff, because I need to be writing my classes with the prototype much more often :)</description>
		<content:encoded><![CDATA[<p>Ah ha!  I see!  You need to do a blog post on all this stuff, because I need to be writing my classes with the prototype much more often <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463202</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:32:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463202</guid>
		<description>Adam,

Ah, I see what you mean. That does indeed work.
The problem with it, however, is that the prototype for MyClass becomes an anonymously created object, where you declare the public method &lt;code&gt;getPrivate&lt;/code&gt; inline in the constructor, instead of using a prototype.

I.e. each instance would have to create its own &lt;code&gt;getPrivate&lt;/code&gt; method, instead of using the one inherited down through the prototype chain.</description>
		<content:encoded><![CDATA[<p>Adam,</p>
<p>Ah, I see what you mean. That does indeed work.<br />
The problem with it, however, is that the prototype for MyClass becomes an anonymously created object, where you declare the public method <code>getPrivate</code> inline in the constructor, instead of using a prototype.</p>
<p>I.e. each instance would have to create its own <code>getPrivate</code> method, instead of using the one inherited down through the prototype chain.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Silver</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463192</link>
		<dc:creator>Adam Silver</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:21:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463192</guid>
		<description>Hi Robert

Can you not do the following:

&lt;code&gt;
			var MyClass = function() {
			}	
			MyClass.prototype = new(function(){
				var privateVar = 1;
				
				function getPrivate() {
					return privateVar;
				}
				
				this.getPrivate = getPrivate;
			});
			var myInstance = new MyClass();
			alert(myInstance.getPrivate()); // this alerts 1
&lt;/code&gt;

I just tested and it seemed to work.</description>
		<content:encoded><![CDATA[<p>Hi Robert</p>
<p>Can you not do the following:</p>
<p><code><br />
			var MyClass = function() {<br />
			}<br />
			MyClass.prototype = new(function(){<br />
				var privateVar = 1;</p>
<p>				function getPrivate() {<br />
					return privateVar;<br />
				}</p>
<p>				this.getPrivate = getPrivate;<br />
			});<br />
			var myInstance = new MyClass();<br />
			alert(myInstance.getPrivate()); // this alerts 1<br />
</code></p>
<p>I just tested and it seemed to work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463175</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:05:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463175</guid>
		<description>Adam,

Interesting thinking!
The problem, however, with that approach is that you can&#039;t access that private variable in any other method for the object. I.e., this won&#039;t work:

&lt;code&gt;
MyClass.prototype.getPrivate = function(){
			    return myPrivateMember;
			};&lt;/code&gt;

With Andrea&#039;s solution above, it is possible with private variables accessible to all methods, as long as they&#039;re declared within the closure, but outside the constructor.

Maybe I should write a post explaining what Andrea touched on above, with private members, to have it more easy to overview than in the comments.</description>
		<content:encoded><![CDATA[<p>Adam,</p>
<p>Interesting thinking!<br />
The problem, however, with that approach is that you can&#8217;t access that private variable in any other method for the object. I.e., this won&#8217;t work:</p>
<p><code><br />
MyClass.prototype.getPrivate = function(){<br />
			    return myPrivateMember;<br />
			};</code></p>
<p>With Andrea&#8217;s solution above, it is possible with private variables accessible to all methods, as long as they&#8217;re declared within the closure, but outside the constructor.</p>
<p>Maybe I should write a post explaining what Andrea touched on above, with private members, to have it more easy to overview than in the comments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Silver</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-463137</link>
		<dc:creator>Adam Silver</dc:creator>
		<pubDate>Wed, 15 Oct 2008 16:23:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-463137</guid>
		<description>Hi there, I actually just sent you an email Robert.

With regards to using private methods and variable on the prototype, I believe you can do the following:
&lt;code&gt;
var MyClass = function() {
}

MyClass.prototype = new (function(){
this.myPublicMember = &quot;public&quot;;
var myPrivateMember = &quot;private&quot;;
});
&lt;/code&gt;

I would love to see a post on using the prototype in much more detail.

Thanks</description>
		<content:encoded><![CDATA[<p>Hi there, I actually just sent you an email Robert.</p>
<p>With regards to using private methods and variable on the prototype, I believe you can do the following:<br />
<code><br />
var MyClass = function() {<br />
}</p>
<p>MyClass.prototype = new (function(){<br />
this.myPublicMember = "public";<br />
var myPrivateMember = "private";<br />
});<br />
</code></p>
<p>I would love to see a post on using the prototype in much more detail.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-462919</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 15 Oct 2008 12:07:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-462919</guid>
		<description>Andrea,

Yeah, exactly my thinking. :-)</description>
		<content:encoded><![CDATA[<p>Andrea,</p>
<p>Yeah, exactly my thinking. <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrea Giammarchi</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-462900</link>
		<dc:creator>Andrea Giammarchi</dc:creator>
		<pubDate>Wed, 15 Oct 2008 11:38:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-462900</guid>
		<description>True Robert, that approach is for private methods, not for private variables.
We could have private variables outside the constructor as well, but those will be shared from every instance, so will be more like private static variables :)</description>
		<content:encoded><![CDATA[<p>True Robert, that approach is for private methods, not for private variables.<br />
We could have private variables outside the constructor as well, but those will be shared from every instance, so will be more like private static variables <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/#comment-462882</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 15 Oct 2008 11:14:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=888#comment-462882</guid>
		<description>Andrea,

Thank you! It passed my mind while writing to Andrew, but naturally, a closure with the constructor (and returning the constructor as well) together with private members is a possible approach.

The only downside, structure-wise, from that is that you can&#039;t define anything private in the actual constructor, and access from somewhere else.</description>
		<content:encoded><![CDATA[<p>Andrea,</p>
<p>Thank you! It passed my mind while writing to Andrew, but naturally, a closure with the constructor (and returning the constructor as well) together with private members is a possible approach.</p>
<p>The only downside, structure-wise, from that is that you can&#8217;t define anything private in the actual constructor, and access from somewhere else.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
