Wanna See Somethin'?

Well, after a bunch of coding to build a all-in-one gallery management and display tool in PHP and MySQL, I’ve gone with a nifty Flash based viewer that seems to work pretty well for what I need. Anyway, there’s a bunch of galeries up at this point. I may do some more tweaking…but until then, it works.

Oh, and if I didn’t mention this before, I got married on August 22nd…

Creating Custom Objects in JavaScript

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

Creating an object is pretty easy. It’s basically your average function with the use of the this keyword.

function myObject()
{
this.myVar = ‘This is MY variable’;
}

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).

var mine = new myObject();

mine is now an instance of myObject(). To reference the property inside the object

var myValue = mine.myVar;

myValue now contains the string ‘This is MY variable.’ Piece of cake.

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).

A Method to the Madness

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 this.

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 substring). 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 substring.

function myObject(someString)
{
       this.string = someString;
}
// How many characters are there?
function get_characterCount()
{
    return this.string.length;
}
// 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;
    }
}
this.stringLength = get_characterCount;
this.search = checkForWordInString;

And here’s how to use your new object

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');

Pretty basic stuff I suppose. This doesn’t really do much, but you get the idea of how to use it.

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.