So, lately I’ve been using classes in PHP 5 and have grown to like the structure and re-use of the code. While at work, I got to thinking about this while working on a JavaScript app for calculating auto loan and lease payments. I figure I can create my very own object in JavaScript and re-use it in many places (since we use about 4 different technologies on the different web sites). I started from scratch and it was surprisingly easy to create an object and reference the different properties and methods in that object. I don’t think you can create private methods, but if you really need to do this, you might as well use a server side solution.
Creating A Simple Object
<p>Creating an object is pretty easy. It’s basically your average function with the use of the <em>this</em> keyword.</p>
<p><p>function myObject()
{
this.myVar = ‘This is MY variable’;
}
<p>And that’s pretty much it. Then all you have to do is call the object like you would any other JavaScript object (like Image(), Array(), etc).</p>
<pre><code>var mine = new myObject();</code></pre>
<p><em>mine</em> is now an instance of myObject(). To reference the property inside the object</p>
<pre><code>var myValue = mine.myVar;</code></pre>
<p><strong>myValue</strong> now contains the string ‘This is MY variable.’ Piece of cake.</p>
<p>This is all well and good, but not much use in this form. It’s too static… let’s make it a tad more dynamic (or something).</p>
A Method to the Madness
<p>Creating methods for your object is just about as easy as creating the object itself. It’s nothing more than nested functions and more of <em>this</em>.</p>
<p>Let’s say you want your object to take a string as input and you want to get some information about that string (like how many characters are in that string and if the string contains a certain <strong>substring</strong>). First we need to create the object, then create a method to count the characters (by using the var.length) and a method that tells us whether or not our string contains a particular <em>substring</em>.</p>
<pre><code>function myObject(someString)
{
this.string = someString;
}
<pre><code>// How many characters are there?
function get_characterCount()
{
return this.string.length;
}
<pre><code>// Does the string contain some other word?
// This method also takes a boolean value (true|false)
// for using case insensitive searching (i.e. blue = Blue = bLuE)
function checkForWordInString(word, caseInSensitive)
{
// default to case insensitive searching
caseInSensitive = (caseInSensitive == ”)
? true:caseInSensitive;
if(caseInSensitive)
{
// convert this.string and word to lower case
return (this.string.toLowerCase().indexOf(word.toLowerCase()) != -1)
? true:false;
}
else
{
// Just search for word in this.string
return (this.string.indexOf(word) != -1)
? true:false;
}
}
<pre><code>this.stringLength = get_characterCount;
this.search = checkForWordInString;
<p>And here’s how to use your new object</p>
<pre><code>var theString = "I'm really tired and should probably go to sleep";
var mine = new myObject(theString);
var theLength = mine.stringLength();
var isTired = mine.search(’tired’);
<p>Pretty basic stuff I suppose. This doesn’t really do much, but you get the idea of how to use it.</p>
<p>Now that I’m really tired, I will go to sleep. Next time I’ll create a real-world example of how this can be used (actually, this gave me an idea for a personal project I’m working)... so, stay tuned.</p>