<?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: Object-Oriented programming with JavaScript</title>
	<atom:link href="http://viralpatel.net/blogs/2009/07/object-oriented-programming-with-javascript.html/feed" rel="self" type="application/rss+xml" />
	<link>http://viralpatel.net/blogs/2009/07/object-oriented-programming-with-javascript.html</link>
	<description>Tutorials, Java, J2EE, Struts, AJAX, JavaScript, CSS, Web 2.0, MySQL, Articles</description>
	<lastBuildDate>Thu, 11 Mar 2010 04:41:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: fernando trasviña</title>
		<link>http://viralpatel.net/blogs/2009/07/object-oriented-programming-with-javascript.html/comment-page-1#comment-10087</link>
		<dc:creator>fernando trasviña</dc:creator>
		<pubDate>Fri, 17 Jul 2009 17:03:09 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1567#comment-10087</guid>
		<description>Well i just want to point that JavaScript instantiation method does not mean copy public methods from the prototype, this process is an implementation of decoration pattern. Every instance creates an empty object that decorates the constructor.prototype object, so actually instance or inheritance (actually done by instance), its a decoration process, this is the process by which JavaScript allows to have dynamic classes, and when you augment the class all previous instances of that class get the method.

i wrote an article on the same topic of Object Oriented JavaScript in Mozilla
https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript</description>
		<content:encoded><![CDATA[<p>Well i just want to point that JavaScript instantiation method does not mean copy public methods from the prototype, this process is an implementation of decoration pattern. Every instance creates an empty object that decorates the constructor.prototype object, so actually instance or inheritance (actually done by instance), its a decoration process, this is the process by which JavaScript allows to have dynamic classes, and when you augment the class all previous instances of that class get the method.</p>
<p>i wrote an article on the same topic of Object Oriented JavaScript in Mozilla<br />
<a href="https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript" rel="nofollow">https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cancel bubble</title>
		<link>http://viralpatel.net/blogs/2009/07/object-oriented-programming-with-javascript.html/comment-page-1#comment-10069</link>
		<dc:creator>cancel bubble</dc:creator>
		<pubDate>Fri, 17 Jul 2009 02:21:05 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1567#comment-10069</guid>
		<description>\&quot;In JavaScript, encapsulation is achieved by the inheritance. The child class inherit all the properties and methods of parent class\&quot;

In JavaScript, when you create a subclass from an existing class, only the *public* and *privileged* members are passed on (public/privileged members are created using the this keyword).  You can also implement encapsulation with private members using closures.

In your example, this.speed is public and accessible, you can change it after instantiating the object.  To truly protect speed, you\&#039;d want to do something like this (I hope the spacing will be preserved):
&lt;pre name=&quot;code&quot; class=&quot;js&quot;&gt;
var Car = function(mph) { 
		//private attribute
		var speed;

		//privileged methods
		this.getSpeed = function() {
		return speed;
	}
	this.setSpeed = function(mph) {
		speed = mph;
	}

	this.setSpeed(mph);
} ;
Car.prototype = { 
	//public, non-privileged methods go here
	whatever: function () {}
};
&lt;/pre&gt;
speed is private, you can only access it using the public getter and setter.  

You can subclass Car and still access speed, but only through the privileged methods (getSpeed and setSpeed) because the privileged methods will be passed on (they are publicly accessible via the this keyword).  No instance methods in the subclass will have *direct* access to speed, though - you have to go through the existing privileged methods (getSpeed and setSpeed).

\&quot;JavaScript supports single class inheritance.\&quot;

JavaScript also has prototypal inheritance which uses objects instead of defining a class structure and technically, you can have multiple inheritance via augmentation/mixins.</description>
		<content:encoded><![CDATA[<p>\&quot;In JavaScript, encapsulation is achieved by the inheritance. The child class inherit all the properties and methods of parent class\&quot;</p>
<p>In JavaScript, when you create a subclass from an existing class, only the *public* and *privileged* members are passed on (public/privileged members are created using the this keyword).  You can also implement encapsulation with private members using closures.</p>
<p>In your example, this.speed is public and accessible, you can change it after instantiating the object.  To truly protect speed, you\&#8217;d want to do something like this (I hope the spacing will be preserved):</p>
<pre name="code" class="js">
var Car = function(mph) {
		//private attribute
		var speed;

		//privileged methods
		this.getSpeed = function() {
		return speed;
	}
	this.setSpeed = function(mph) {
		speed = mph;
	}

	this.setSpeed(mph);
} ;
Car.prototype = {
	//public, non-privileged methods go here
	whatever: function () {}
};
</pre>
<p>speed is private, you can only access it using the public getter and setter.  </p>
<p>You can subclass Car and still access speed, but only through the privileged methods (getSpeed and setSpeed) because the privileged methods will be passed on (they are publicly accessible via the this keyword).  No instance methods in the subclass will have *direct* access to speed, though &#8211; you have to go through the existing privileged methods (getSpeed and setSpeed).</p>
<p>\&quot;JavaScript supports single class inheritance.\&quot;</p>
<p>JavaScript also has prototypal inheritance which uses objects instead of defining a class structure and technically, you can have multiple inheritance via augmentation/mixins.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
