randys.org

Wasting your precious bandwidth since 1999

Archive for August, 2004

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…

• • •

This site looks like shit

I just checked this site in IE 6 (Win2k) the other day. I noticed that it looks like shit on some pages. I can’t help that. I refuse to write code (for my personal site) that caters to likes of IE.

On a similar note, it looks like MSIE 7 may come out before Foghorn …or Loghorn …or Laghorn ..or whatever the hell M$ is calling it. Just what we needed… another shitty browser to code for.

• • •

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

<p>Creating an object is pretty easy. It&#8217;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&#8217;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 &#8216;This is MY variable.&#8217; Piece of cake.</p>


<p>This is all well and good, but not much use in this form. It&#8217;s too static&#8230; let&#8217;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&#8217;s nothing more than nested functions and more of <em>this</em>.</p>



<p>Let&#8217;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&#8217;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&#8217;t really do much, but you get the idea of how to use it.</p>


<p>Now that I&#8217;m really tired, I will go to sleep. Next time I&#8217;ll create a real-world example of how this can be used (actually, this gave me an idea for a personal project I&#8217;m working)... so, stay tuned.</p>
• • •

All content Copyright © Randy Sesser | Hosted by WebFaction
Entries (RSS) | Comments (RSS)

randys.org is Digg proof thanks to caching by WP Super Cache!