<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FSimple_OOP</id>
		<title>JavaScript/Simple OOP - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FSimple_OOP"/>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;action=history"/>
		<updated>2026-05-25T04:37:43Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.0</generator>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=823&amp;oldid=prev</id>
		<title>Apollolux: aside</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=823&amp;oldid=prev"/>
				<updated>2013-06-14T01:09:19Z</updated>
		
		<summary type="html">&lt;p&gt;aside&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 01:09, 14 June 2013&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;There is an alternate article for this that is more thorough, but uses more jargon and is probably more suited to more advanced programmers. This more advanced article covers advanced concepts such as Polymorphism and Inheritance. See [[Object oriented programming]].&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;{{aside|Note|&lt;/ins&gt;There is an alternate article for this that is more thorough, but uses more jargon and is probably more suited to more advanced programmers. This more advanced article covers advanced concepts such as Polymorphism and Inheritance. See [[Object oriented programming]].&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;|note.svg}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;So you heard a lot about OOP and decided to check it out? Good, that's why we're here. I'm assuming you know ''a bit'' about JavaScript syntax, but this shouldn't be too hard, just things like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;So you heard a lot about OOP and decided to check it out? Good, that's why we're here. I'm assuming you know ''a bit'' about JavaScript syntax, but this shouldn't be too hard, just things like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=821&amp;oldid=prev</id>
		<title>Apollolux: Apollolux moved page Simple OOP in JavaScript to JavaScript/Simple OOP: new conventions</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=821&amp;oldid=prev"/>
				<updated>2013-06-14T01:08:16Z</updated>
		
		<summary type="html">&lt;p&gt;Apollolux moved page &lt;a href=&quot;/index.php?title=Simple_OOP_in_JavaScript&quot; class=&quot;mw-redirect&quot; title=&quot;Simple OOP in JavaScript&quot;&gt;Simple OOP in JavaScript&lt;/a&gt; to &lt;a href=&quot;/index.php?title=JavaScript/Simple_OOP&quot; title=&quot;JavaScript/Simple OOP&quot;&gt;JavaScript/Simple OOP&lt;/a&gt;: new conventions&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 01:08, 14 June 2013&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;' lang='en'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=807&amp;oldid=prev</id>
		<title>Apollolux: link text</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=807&amp;oldid=prev"/>
				<updated>2013-06-14T00:18:25Z</updated>
		
		<summary type="html">&lt;p&gt;link text&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 00:18, 14 June 2013&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot; &gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;So you heard a lot about OOP and decided to check it out? Good, that's why we're here. I'm assuming you know ''a bit'' about JavaScript syntax, but this shouldn't be too hard, just things like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;So you heard a lot about OOP and decided to check it out? Good, that's why we're here. I'm assuming you know ''a bit'' about JavaScript syntax, but this shouldn't be too hard, just things like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;''If you want to learn, but don't want to shovel through text, go to [[#I'm dumb...]]''&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;''If you want to learn, but don't want to shovel through text, go to [[#&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;I'm dumb...|&lt;/ins&gt;I'm dumb...]]''&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;''If you know a bit about classes but don't like explanations, go to [[#I'm lazy...]]''&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;''If you know a bit about classes but don't like explanations, go to [[#&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;I'm lazy...|&lt;/ins&gt;I'm lazy...]]''&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If you want the whole explanations thing, as well as two ways to define methods, just read through the example.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If you want the whole explanations thing, as well as two ways to define methods, just read through the example.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=806&amp;oldid=prev</id>
		<title>Apollolux: created from http://web.archive.org/web/20120828183913/http://www.spheredev.org/wiki/Simple_OOP_in_JavaScript</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Simple_OOP&amp;diff=806&amp;oldid=prev"/>
				<updated>2013-06-14T00:17:44Z</updated>
		
		<summary type="html">&lt;p&gt;created from http://web.archive.org/web/20120828183913/http://www.spheredev.org/wiki/Simple_OOP_in_JavaScript&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;There is an alternate article for this that is more thorough, but uses more jargon and is probably more suited to more advanced programmers. This more advanced article covers advanced concepts such as Polymorphism and Inheritance. See [[Object oriented programming]].&lt;br /&gt;
&lt;br /&gt;
So you heard a lot about OOP and decided to check it out? Good, that's why we're here. I'm assuming you know ''a bit'' about JavaScript syntax, but this shouldn't be too hard, just things like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;function&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
''If you want to learn, but don't want to shovel through text, go to [[#I'm dumb...]]''&lt;br /&gt;
&lt;br /&gt;
''If you know a bit about classes but don't like explanations, go to [[#I'm lazy...]]''&lt;br /&gt;
&lt;br /&gt;
If you want the whole explanations thing, as well as two ways to define methods, just read through the example.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==What is OOP?==&lt;br /&gt;
OOP = '''Object-oriented programming'''&lt;br /&gt;
&lt;br /&gt;
It's using these things called ''objects'' to do stuff. An object is just like a car, or a ball, or a person. It has '''properties''': info that describes it, e.g.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
person.hairColour = CreateColor(0, 0, 0); // black hair&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Objects also have methods: things that the object can do, e.g.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
person.jump(5);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Why should I care about OOP?==&lt;br /&gt;
Two reasons. First, it's a very natural way of looking at things. It helps a lot in games, because objects occur very naturally in them too, like the player, a spaceship, a sword, shield, an enemy, and so on. Sphere makes games.&lt;br /&gt;
&lt;br /&gt;
Second, it makes your JavaScript code much neater and easier to handle later on. OOP stops you repeating code and so lets you type less. Less typing = less errors = faster completion of your game. It grows easier too.&lt;br /&gt;
&lt;br /&gt;
==I'm dumb...==&lt;br /&gt;
This is speed mode. From zero to hero in ten minutes.&lt;br /&gt;
&lt;br /&gt;
Objects are like things in the real world. Things have '''properties''', e.g. person has an eyeColor, and they have '''methods''', e.g. person can clap.&lt;br /&gt;
&lt;br /&gt;
Here's how we can make a simple Person object:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function Person(name, eyeColor) {&lt;br /&gt;
  this.name = name;&lt;br /&gt;
  this.eyeColor = eyeColor;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can now make Persons:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var bob = new Person(&amp;quot;Bob&amp;quot;, &amp;quot;blue&amp;quot;);&lt;br /&gt;
var ted = new Person(&amp;quot;Ted&amp;quot;, &amp;quot;teal&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can get properties:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var a = bob.name     // a now contains &amp;quot;Bob&amp;quot;&lt;br /&gt;
var b = ted.eyeColor // b now contains &amp;quot;teal&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can set properties:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
bob.eyeColor == &amp;quot;brown&amp;quot;  // false&lt;br /&gt;
bob.eyeColor = &amp;quot;brown&amp;quot;;&lt;br /&gt;
bob.eyeColor == &amp;quot;brown&amp;quot;  // true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can make methods. They're like functions, but for an object:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
Person.prototype.clap = function () {&lt;br /&gt;
  Abort(&lt;br /&gt;
      &amp;quot;This was a silence game.\n&amp;quot; +&lt;br /&gt;
      &amp;quot;You just lost, &amp;quot; + this.name + &amp;quot;.\n&amp;quot; +&lt;br /&gt;
      &amp;quot;Game over.\n&amp;quot; );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can now call methods.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
ted.clap();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That will show:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This was a silence game.&lt;br /&gt;
You just lost, Ted.&lt;br /&gt;
Game over.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That's it!&lt;br /&gt;
&lt;br /&gt;
==Example: kicking people==&lt;br /&gt;
The choice might be my misanthropy showing. Oh well.&lt;br /&gt;
&lt;br /&gt;
In this example, we'll make a MalePerson object, capable of kicking, and being kicked.&lt;br /&gt;
&lt;br /&gt;
===The MalePerson template===&lt;br /&gt;
Let's write some code:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function MalePerson(name) {&lt;br /&gt;
  this.name = name;&lt;br /&gt;
  this.maxHitPoints = 20;&lt;br /&gt;
  this.hitPoints = this.maxHitPoints;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is like a template for all MalePersons. They all have a ''name'', they all have ''maxHitPoints'' and they all have ''hitPoints''. Those are all called '''properties'''.&lt;br /&gt;
&lt;br /&gt;
Objects that come out of the same template can have different properties, e.g. one MalePerson object could have a name of &amp;quot;Bob&amp;quot;, while another has a name of &amp;quot;Jim&amp;quot;. You'll see this when we make a few objects from this template.&lt;br /&gt;
&lt;br /&gt;
===MalePerson methods===&lt;br /&gt;
'''Methods''' are a way for your objects to 'do' things, like 'kick' or 'takeDamage'. They're just functions, but connected to an object.&lt;br /&gt;
&lt;br /&gt;
There are 3 ways of making methods. I'll only cover one.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Kick another MalePerson.&lt;br /&gt;
 * @param  victim  The other MalePerson object.&lt;br /&gt;
 * @param  location  String, place on body to kick the other person.&lt;br /&gt;
 */&lt;br /&gt;
MalePerson.prototype.kick = function (victim, location) {&lt;br /&gt;
  victim.takeDamage(location, 11);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Take damage to an area.&lt;br /&gt;
 * @param  area  String, the location of the damage.&lt;br /&gt;
 * @param  amount  Number, normal amount of damage taken.&lt;br /&gt;
 */&lt;br /&gt;
MalePerson.prototype.takeDamage = function (location, amount) {&lt;br /&gt;
  if (location == &amp;quot;groin&amp;quot;)&lt;br /&gt;
    this.hitPoints = this.hitPoints - amount * 2;&lt;br /&gt;
  else&lt;br /&gt;
    this.hitPoints = this.hitPoints - amount;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That stuff in /* */ is just for people like you and me. JavaScript ignores them, so you can put whatever in them, or leave them out if you want.&lt;br /&gt;
&lt;br /&gt;
Notice the use of '''prototype''', along with '''function'''. This way, all MalePerson objects will gain the kick and takeDamage methods. See how methods are set as functions?&lt;br /&gt;
&lt;br /&gt;
===Making objects===&lt;br /&gt;
That's all fine and well, we've prepared a template. But by itself, a template doesn't do anything. How do we use the template? Easy, just watch and learn.&lt;br /&gt;
&lt;br /&gt;
Let's make a couple of MalePersons:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var timmy = new MalePerson(&amp;quot;Timmy&amp;quot;);&lt;br /&gt;
var thug = new MalePerson(&amp;quot;Greasy Thug&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This how we make '''new''' MalePerson objects. Get it? :)&lt;br /&gt;
&lt;br /&gt;
Each object is distinct, just like one ball in real life is different from another. Going with the above two lines, see if you can figure out these:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
timmy.name == &amp;quot;Timmy&amp;quot;       // true&lt;br /&gt;
thug.name == &amp;quot;Greasy Thug&amp;quot;  // true&lt;br /&gt;
timmy.name == thug.name     // false&lt;br /&gt;
timmy.name != thug.name     // true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yep, these guys have different names. This is how we get properties from objects.&lt;br /&gt;
&lt;br /&gt;
===Using objects===&lt;br /&gt;
Now let's make our MalePersons again, and one will kick the other.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
// These two lines were from above.&lt;br /&gt;
var timmy = new MalePerson(&amp;quot;Timmy&amp;quot;);&lt;br /&gt;
var thug = new MalePerson(&amp;quot;Greasy Thug&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
thug.kick(timmy, &amp;quot;thigh&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Abort(&lt;br /&gt;
    timmy.hitPoints + &amp;quot;HP left for Timmy\n&amp;quot; +&lt;br /&gt;
    thug.hitPoints + &amp;quot;HP left for Greasy Thug.\n&amp;quot; );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Okay, poor Timmy has just been kicked by our Greasy Thug. This is the output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
9HP left for Timmy&lt;br /&gt;
20HP left for Greasy Thug&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first bold line is where the Greasy Thug '''kick'''ed Timmy.&lt;br /&gt;
&lt;br /&gt;
The second bold bit is how we access Timmy's '''hitPoints''' property, showing how hurt he is.&lt;br /&gt;
&lt;br /&gt;
The third bold bit is how we access the Greasy Thug's '''hitPoints''' property. He hasn't been hurt a bit.&lt;br /&gt;
&lt;br /&gt;
Can you figure out how to display the names on the last lines like the hitPoints as well, instead of directly typing &amp;quot;Timmy&amp;quot; and &amp;quot;Greasy Thug&amp;quot;?&lt;br /&gt;
&lt;br /&gt;
===Bringing it together===&lt;br /&gt;
Let's put this all together, shall we? The big method comments have been left out for readability.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function MalePerson(name) {&lt;br /&gt;
  this.name = name;&lt;br /&gt;
  this.maxHitPoints = 20;&lt;br /&gt;
  this.hitPoints = this.maxHitPoints;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
MalePerson.prototype.kick = function (victim, location) {&lt;br /&gt;
  victim.takeDamage(location, 11);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
MalePerson.prototype.takeDamage = function (location, amount) {&lt;br /&gt;
  if (location == &amp;quot;groin&amp;quot;)&lt;br /&gt;
    this.hitPoints = this.hitPoints - amount * 2;&lt;br /&gt;
  else&lt;br /&gt;
    this.hitPoints = this.hitPoints - amount;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now let's give Timmy a chance for revenge:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var timmy = new MalePerson(&amp;quot;Timmy&amp;quot;);&lt;br /&gt;
var thug = new MalePerson(&amp;quot;Greasy Thug&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
timmy.kick(thug, &amp;quot;groin&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Abort(thug.HitPoints);  // Shows -2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Uh oh, Timmy better do some fast running: he just ''killed'' the thug.&lt;br /&gt;
&lt;br /&gt;
==What did I just learn?==&lt;br /&gt;
Here's the lingo, if you've heard about classes before:&lt;br /&gt;
&lt;br /&gt;
*	objects ('''new''' ObjectName())&lt;br /&gt;
*	properties (with '''this.'''blah)&lt;br /&gt;
*	methods (obj.'''kick()''')&lt;br /&gt;
&lt;br /&gt;
And don't kick MalePersons in the groin. It hurts.&lt;br /&gt;
&lt;br /&gt;
==I'm ambitious!==&lt;br /&gt;
[[Advanced OOP in JavaScript]] covers inheritance, e.g. using a Person function to make MalePerson and FemalePerson, superclasses (Person) and subclasses (MalePerson and FemalePerson), getters and setters, as well as saving code using polymorphism, and a trick to allow calling superclass methods from the subclasses.&lt;br /&gt;
&lt;br /&gt;
==I'm lazy...==&lt;br /&gt;
The example was big, so here's the cheat sheet.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function MyObjectName(param1, param2) {&lt;br /&gt;
  // Properties&lt;br /&gt;
  this.property1 = param1;&lt;br /&gt;
  this.property2 = param2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Methods&lt;br /&gt;
MyObjectName.prototype.method1 = function () {&lt;br /&gt;
  // Code for method1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
MyObjectName.prototype.method2 = function () {&lt;br /&gt;
  // Code for method2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
// Making objects&lt;br /&gt;
var object = new MyObjectName(12, &amp;quot;blah&amp;quot;);&lt;br /&gt;
var object2 = new MyObjectName(11, &amp;quot;haha&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// Using object properties&lt;br /&gt;
if (object.property2 == &amp;quot;blah&amp;quot;)&lt;br /&gt;
  object.property2 = &amp;quot;blah was lame anyway&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Using object methods&lt;br /&gt;
object.method1();&lt;br /&gt;
object2.method2(3);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cheers! --Tunginobi 10:22, 28 January 2007 (GMT)&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	</feed>