Posted on March 29, 2007, 12:10 pm, by Dan, under
black belt.
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 [...]
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 [...]
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 [...]
Posted on March 22, 2007, 4:53 pm, by Dan, under
black belt.
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 [...]
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 [...]
[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 [...]