<?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>Komodo Media &#187; class</title>
	<atom:link href="http://www.komodomedia.com/tags/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.komodomedia.com</link>
	<description>blog, webdesign, illustration &#38; newsk00l javascript</description>
	<lastBuildDate>Wed, 18 Jan 2012 14:43:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript classes for n00bs</title>
		<link>http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/</link>
		<comments>http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 02:49:08 +0000</pubDate>
		<dc:creator>Rogie</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nerdlab]]></category>
		<category><![CDATA[class]]></category>

		<guid isPermaLink="false">http://www.komodomedia.com/?p=280</guid>
		<description><![CDATA[Ok. Simple and sweet. I wish I would have known about this a few years ago.  I&#8217;m talking about the idea of a JavaScript function that can be used like a class.  Here&#8217;s what I want:

Simple class interface that I can instantiate with new Classname
Public functions/members
Private functions/members
Priveleged functions/members
Static functions/members

Lately I&#8217;ve been using JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>Ok. Simple and sweet. I wish I would have known about this a few years ago.  I&#8217;m talking about the idea of a JavaScript function that can be used like a class.  Here&#8217;s what I want:</p>
<ul>
<li>Simple class interface that I can instantiate with <code class="inline">new Classname</code></li>
<li>Public functions/members</li>
<li>Private functions/members</li>
<li>Priveleged functions/members</li>
<li>Static functions/members</li>
</ul>
<p>Lately I&#8217;ve been using JavaScript functions as class objects and utilizing private, priveleged and static members and functions to my hearts content.  Let me share.</p>
<p><small><strong>Disclaimer:</strong> I don&#8217;t consider myself a JavaScript guru, nor do I think that this is the best solution evar known to mankind.  If you have a snooty comment to say, please don&#8217;t leave it here. If you have a piece of constructive criticism or have better methods, do tell. I&#8217;d love to hear what you use. If your JavaScript knowledge is more l33t, then by all means, humbly share.</small></p>
<h3>A Basic Class &amp; Instance</h3>
<p><code>function MyClass () {<br />
   //...<br />
}<br />
var oClass = new MyClass();</code></p>
<h3>Public Member Variables &amp; Functions</h3>
<p>Public functions and variables are available to access on an instance of a class.<br />
<code>function MyClass () {<br />
   //..<br />
}<br/><br />
//public member variable<br />
MyClass.prototype.publicVar =  "My Public Variable";<br/><br />
//public member function<br />
MyClass.prototype.publicFunction = function () {<br />
      alert( this.publicVar );<br />
}<br/><br />
//create an instance<br />
var oClass = new MyClass();<br/><br />
//run a member function<br />
oClass.publicFunction();   //Alert: "My Public Variable"<br />
</code></p>
<h3>Private Member Variables &amp; Functions</h3>
<p>Private member functions and variables are hidden to outside code. Only public functions can access them.<br />
<code>function MyClass () {<br/><br />
   //reference to this<br />
   var self = this;<br/><br />
   //private member variable<br />
   var privateVar = "My Private Variable";<br/><br />
   //public member variable<br />
   this.publicVar = "My Public Variable";<br/><br />
   //private member function<br />
   var privateFunction = function () {<br/><br />
      self.publicVar += " Modified By A Private Fucntion";<br/><br />
      alert( self.publicVar );<br/><br />
   }<br />
}<br />
//create an instance<br />
var oClass = new MyClass();<br/><br />
//run a private member function<br />
oClass.privateFunction();   //private function is undefined<br/><br />
//get a private member var<br />
alert( oClass.privateVar );   //private var is undefined<br />
</code></p>
<h3>Static Member Variables &amp; Functions</h3>
<p>A static function or variable is available on the base class (or JavaScript) function, but is not available to the class instance.<br />
<code>function MyClass () {<br />
   //...<br />
}<br/><br />
//declare a static member<br />
MyClass.staticVar = "My static variable";<br/><br />
//declare a static function<br />
MyClass.staticFunction = function ( pInput ) {<br />
   return new MyClass( MyClass.staticVar , pInput );<br />
}<br/><br />
//create an instance<br />
var oClass = new MyClass();<br/><br />
//run a static function (NO access to private or public)<br />
oClass.staticFunction( 9 );  //staticFunction is undefined on an instance<br />
//run a privileged member function on the class<br />
MyClass.privilegedFunction();   //The function runs<br />
</code></p>
<h3>Priveleged Member Variables &amp; Functions</h3>
<p>A privileged member function has access to private variables, but is available publicly.<br />
<code>function MyClass () {<br/><br />
   //private member variable<br />
   var privateVar = "My Private Variable";<br/><br />
   //privileged member function<br />
   this.privilegedFunction = function () {<br />
      alert( privateVar );<br />
   }<br />
}<br />
//create an instance<br />
var oClass = new MyClass();<br/><br />
//run a privileged member function<br />
oClass.privilegedFunction();   //Output: alerts the value of the private var<br/><br />
</code></p>
<p>These classes may come as news to you&#8230;or maybe you&#8217;ve known about them since you were a fetus.  In either circumstance, they&#8217;ve been a big help to me.</p>
<p>Word up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: komodomedia.com @ 2012-02-10 13:42:15 by W3 Total Cache -->
