Archive for the ‘black belt’ Category

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 [...]

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 [...]

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 [...]

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 [...]

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 [...]

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 [...]