January 14, 2012

Javascript and it's tricks

I love coding in Python, C++, Java and a few other languages, and I love it because of the challenge of using every language's little dirty tricks. For example, I love how Python decorators and lambda functions work and I love how Java Reflections work, just to mention some of them, and how many possibilities (scary bugs and low performance too :p)  bring us those kind of functions. But the one thing that I most love about all those languages is that they are used for desktop applications.
Desktop development is what I love the most. But I'm forced (read as "that's the job I have") to code in PHP + Javascript. Don't get me wrong, those languages are fine too (better than the language that co-workers used before I got here: ASP .Net...) and they sure have their special things, but it's just that I hate web-development.

Anyway, as I'm forced to do PHP + Javascript on a daily basis, and I got tired of trying to make stuff work as I expect them to work in all browsers, I made a little set of Javascript scripts. Nothing as powerful as jQuery or MooTools, but something that, in combination with those frameworks, makes my life easier.

Basically, those scripts check if the browser they run on has certain methods, and if not, they add it via prototype.
Every object constructor exposes a special property called “prototype.” That property allows you to add properties (and methods) to all objects instantiated from that constructor function.
Means, for example, the String type of data has some methods, like .substring() or .trim()... The "prototype" property gives us the ability to check if a method of String exists, and if doesn't, create our own. So that's what my scripts do.

There are some pretty cool functions for strings, numbers, dates, arrays and functions itself.

If you're interested in Javascript or you work with Javascript, have a quick look: JniftilitieS

Oh, just one more thing: Yes, I do know about the existence of IE7, IE8 and IE9 compatibility scripts. Mine are a little bit different, as they also add customized functions, like String.capitalizeAllWords() or String.toInt()...