'black belt' category

3 things that javascript and Ruby have in common

When I decided to start Javascript Kata, a friend of mine just thought it was a cool idea and started his own site, Ruby Fleebie. I’ve never written code in Ruby but it looks like a very pleasant language. He borrowed me Agile Web Development with Rails: A Pragmatic Guide and I really liked the […]

Read the complete article...

How to make a singleton in javascript

Singleton is one of the most common and easiest design pattern. In fact, a lot of designs are wrong just because the developer didn’t know about singletons and design patterns (I could have said : a lot of my designs were wrong just because I didn’t know about singletons and design patterns).
I was looking at […]

Read the complete article...

Looping through object properties and hash tables using the “in” statement

As I said earlier, objects are hash tables of properties and functions so looping throught them is done the same way (weepee!).
The “in” statement
The in statement must be used in a for loop. Like this : for (var element in allElements). In most of the other languages, the element variable would contain the complete element […]

Read the complete article...

How to use javascript hashes (or hash-table)

Javascript has no hash table object but you can emulate one by creating a simple object and using brackets.
[source:javascript]
var hashPetName = new Object();
hashPetName[”dog”] = “Rex”;
hashPetName[”cat”] = “Mistigri”;
alert(”My dog’s name is ” + hashPetName[”dog”]
+ ” and my cat’s name is ” + hashPetName[”cat”] );
[/source]
Great! That’s it about javascript hashes, now I can go groom my cat.
Wait!!!
Why […]

Read the complete article...

How to do class functions in javascript (aka static or shared functions)

One of the really concept of the object-oriented programming is the class functions but they are also known as static functions (Java, php, C++) or shared functions (.NET).
Why using class method?
Class methods help you keeping your code clean by centralizing your functions. Example, you create a calendar object that formats a string from two dates […]

Read the complete article...

How to use anonymous objects

I don’t know how to call it and I don’t even know if it has a name. I thought a moment about calling it Mistigri but I already have a cat with that name. I decided to call it anonymous object.
How?
[source:javascript]
var ao = {
name : “Anonymous Object”,
possibleName : “Mistigri”,
presentYourself : function() {
alert(”Hi! My name is […]

Read the complete article...

How to do enumerations (enum) in javascript

One of the most common mistake I see when I use a third-party javascript library is the enormous amount of “constants” that is 152 characters to ensure its uniqueness. Example, a color chooser named KikaColorChooser. If it has a constant for the type of display, it will be named something like KIKACOLORCHOOSER_DISPLAY_TYPE_SMALL (a constant of […]

Read the complete article...

How to extend javascript classes

The intrinsec objects of javascript (String, Number, Date, etc) are missing a lot of handy methods. God knows why. Example, you don’t have a trim() function on a String object. Maybe the developers thought that it was easy enough to write theString.replace(/^\s*|\s*$/g, “”) to trim a string but that’s not the kind of ugly code […]

Read the complete article...

What are javascript prototypes? (short answer for advanced javascripters)

[I’ve also written a longer answer for beginners] 
Prototypes can extend any class you want by adding a property or a method. By calling,
[source:javascript]
String.prototype.alertMe = function() {
alert(this);
}
[/source]
you are adding the method alertMe() to every String object of your application.
It uses less memory because javascript creates only one instance of the function and uses references to it.
Short […]

Read the complete article...