<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.toString() &#187; prototype</title>
	<atom:link href="http://www.dottostring.com/tag/prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dottostring.com</link>
	<description>Programming is our passion</description>
	<lastBuildDate>Sun, 04 Oct 2009 15:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use prototype to add Properties/Methods to a class in Javascript</title>
		<link>http://www.dottostring.com/2008/11/how-to-use-prototype-to-add-propertiesmethods-to-a-class-in-javascript/</link>
		<comments>http://www.dottostring.com/2008/11/how-to-use-prototype-to-add-propertiesmethods-to-a-class-in-javascript/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 20:30:26 +0000</pubDate>
		<dc:creator>Irfan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[Object Oriented]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.dottostring.com/?p=88</guid>
		<description><![CDATA[I discussed the basics of Object Oriented Javascript Programming in my previous post. Now lets extend a class to add some more properties and methods to its definition. 
Every object constructor has a special property &#8216;prototype&#8217; in Javascript (Please do not confuse it with prototype javascript library). Lets use it:

function myClass() {
}
myClass.prototype.aProperty = "foo";
var myObject = [...]]]></description>
			<content:encoded><![CDATA[<p>I discussed the <a title="Basics of Object Oriented Programming in Javascript" href="/2008/11/basics-of-object-oriented-programming-in-javascript/">basics of Object Oriented Javascript Programming</a> in my previous post. Now lets extend a class to add some more properties and methods to its definition. </p>
<p>Every object constructor has a special property <em>&#8216;prototype&#8217;</em> in Javascript (Please do not confuse it with <a href="http://www.prototypejs.org/" target="_blank">prototype javascript library</a>). Lets use it:</p>
<pre name="code" class="js">
function myClass() {
}
myClass.prototype.aProperty = "foo";
var myObject = new myClass();
alert(myObject.aProperty);
</pre>
<p>This adds aProperty to the definition of myClass. The <em>&#8216;prototype&#8217;</em> adds this property to all the objects whether they have been created or not. So, this example will work too:</p>
<pre name="code" class="js">
function myClass() {
}
var myObject = new myClass();
myClass.prototype.aProperty = "foo";
alert(myObject.aProperty);
</pre>
<p>The <em>&#8216;prototype&#8217;</em> object loads before the object&#8217;s constructor does anything.</p>
<pre name="code" class="js">
function myClass() {
	this.aProperty = "bar";
}
var myObject = new myClass();
myClass.prototype.aProperty = "foo";
alert(myObject.aProperty);
// this alert will show "bar"
</pre>
<p>So, using <em>&#8216;prototype&#8217;</em> we cannot override the properties and methods already defined in the object&#8217;s constructor.</p>
<p>We can use the <em>&#8216;prototype&#8217;</em> to add functionalities to the already existing classes. Lets add some functionality to the String class.</p>
<pre name="code" class="js">
String.prototype.vowelLength = function() {
	var count = 0;
	for(i=0; i &lt; this.length; i++) {
		var l = this.charAt(i); 
		if(l == "a" || l == "A" || l == "e" || l == "E" || l == "i" || l == "I" || l == "o" || l == "O" || l == "u" || l == "U") {
			count++;
		}
	}
	return count;
}
var myString = "United States of America";
alert(myString.vowelLength());
</pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.dottostring.com%2F2008%2F11%2Fhow-to-use-prototype-to-add-propertiesmethods-to-a-class-in-javascript%2F';
  addthis_title  = 'How+to+use+prototype+to+add+Properties%2FMethods+to+a+class+in+Javascript';
  addthis_pub    = 'erfaan';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.dottostring.com/2008/11/how-to-use-prototype-to-add-propertiesmethods-to-a-class-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
