Archive for the ‘javascript’ Category

Javascript String Replace

Tuesday, September 11th, 2007

I promise myself that at some point I will go back and re-read this article by Ben Nadel.

Modifying the String class in Javascript

Tuesday, September 11th, 2007

Ivan Uzunov has posted his top 10 modifications to the String prototype in Javascript. While all modifications to the prototype of one of Javascript’s built-in classes is fraught with complications, some of these methods seem very useful. I wish they were in the core of the Javascript language. To split an array into a string is something I do all the time in PHP, so I wish Javascript had something like this:

This extension splits the string by given separator and returns an array with trimmed items. It uses the trim() extension above:

String.prototype.splitrim = function(t){ return this.trim().split(new RegExp(’\\s*’+t+’\\s*’)) }//test splitrim
test = ‘ testing , splitrim ‘;
var arr = test.splitrim(’,’);
document.write (’”’ + arr[0] + ‘”’);
document.write (’”’ + arr[1] + ‘”’);

Javascript as an Object Oriented Programming language

Monday, September 10th, 2007

I’m a little amazed at the ability of Javascript to mimic other languages. Prototype has added some features that allow Javascript to imitate some aspects of Ruby. This article gives Javascript a set of methods that allow a close imitation of standard object inheritance, as seen in languages like Java or C#. The author offers the standard qualifiers:

The following is a discussion of a technique I’ve used over the years to simulate the feeling of Object-Oriented Programming in languages like C# and Java. The keyword here is “simulation”. This is not necessarily equivalent to what you would encounter in those languages. JavaScript is a very expressive language and, as a result, there are a wide variety of techniques you can employ to build re-usable and extensible code. What I describe here is by no means the final answer on that subject.

As a side note, this article will use OOP terminology common to Java and C#; however, this vernacular is technically incorrect when discussing JavaScript in general. So, if you find it offensive to mix that terminology with JS then close this page now! For the rest of you, keep in mind that I am merely presenting an idiom. I tend to build libraries, so this particular style of JS coding fits my needs well. Others will argue that it is pointless to write JS in this way. If you decide to use this approach or even to roll your own OOP JS, be prepared for debates much like the tab vs. whitespace argument.