<?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>Delivering Quality &#187; OOP</title>
	<atom:link href="http://www.markdeepwell.com/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markdeepwell.com</link>
	<description>A Software Developers View of the World</description>
	<lastBuildDate>Sat, 31 Dec 2011 22:41:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Intro to Objects in JavaScript</title>
		<link>http://www.markdeepwell.com/2009/10/intro-to-objects-in-javascript/</link>
		<comments>http://www.markdeepwell.com/2009/10/intro-to-objects-in-javascript/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 06:23:03 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.markdeepwell.com/?p=317</guid>
		<description><![CDATA[Despite what you might think, JavaScript supports programming in full object oriented style including inheritance, encapsulation and polymorphism. But before we dive into all those topics I&#8217;m going to start with the basics and demonstrate how to create and use a simple JavaScript object. Lets say you have a web application that needs to track [...]]]></description>
			<content:encoded><![CDATA[<p>Despite what you might think, JavaScript supports programming in full object oriented style including inheritance, encapsulation and polymorphism. But before we dive into all those topics I&#8217;m going to start with the basics and demonstrate how to create and use a simple JavaScript object.</p>
<p>Lets say you have a web application that needs to track how long an AJAX request took. If you were writing it in Ruby, PHP or Python you&#8217;d have a timer object that you can start() and stop() and a seconds() method to get the number of seconds that passed between calling the first two methods.</p>
<p>To build this in JavaScript we first create our constructor:</p>
<pre class="brush: jscript; title: ; notranslate">
Timer = function() {
  var seconds_passed = 0;
}
</pre>
<p>Here we are creating a Timer object with a private  seconds_passed variable. To create this object we we simply create a new timer:</p>
<pre class="brush: jscript; title: ; notranslate">
var elapsed_timer = new Timer();
</pre>
<p>This timer object now needs some public methods. We can attach a public method by using this.method_name:</p>
<pre class="brush: jscript; title: ; notranslate">
Timer = function() {
  var seconds_passed = 0;

  this.start = function() {
    // implementation here
  }
}
</pre>
<p>Our Timer class now has a start() function. Additional public methods can be added in the same way.</p>
<p>To add a private method, don&#8217;t define it with the <em>this</em> keyword but instead create a regular function inside the class:</p>
<pre class="brush: jscript; title: ; notranslate">
Timer = function() {
  function increment() {
    // implementation of private method here
  }
}
</pre>
<p>Now we have a Timer object with a public start method and a private increment method. The rest of the class is fairly simple, so lets jump to the finished solution.</p>
<h3>Complete Example</h3>
<pre class="brush: jscript; title: ; notranslate">
Timer = function() {
  var timeout_id = null;
  var seconds_passed = 0;

  this.start = function() {
    increment();
  }

  this.stop = function() {
    clearTimeout(timeout_id);
    timeout_id = null;
  }

  this.seconds = function() {
    return seconds_passed;
  }

  function increment() {
    seconds_passed++;

    timeout_id = window.setTimeout(
      function() {
        increment();
      }
      , 1000
    );
  }
}

var elapsed_timer = new Timer();
elapsed_timer.start();

// do some stuff

elapsed_timer.stop();
alert(elapsed_timer.seconds());
</pre>
<p>There you have it. A simple JavaScript timer to keep track of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markdeepwell.com/2009/10/intro-to-objects-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

