I actually LIKE Javascript

So, I decided to make a personal website, and of course, that requires a little bit of javascripting.

This time, I didn’t jump into Angular or nodeJs or any of the tools cool kids now use, I started off with pure Javascript for a few utility and once I got a feel for it I moved on to JQuery.

And I actually came to LIKE Javascript.

I’ve always been a C++/C# guy, the flexibility C/C++ gives with loose type and pointers just make so much sense, but I also loved the library .Net provides. I guess I like C/C++ for their concepts, and C#/.Net for its practicality/productivity.

So, Javascript has never really been on my radar even though I do have to work with it from time to time.

This time, I decided to properly study javascript, learn how to use prototype, how to simulate OOP using javascript and decided that it’s a really cool language.

So here’s what I like most about Javascript

1) It’s flexible like C/C++

The one thing I missed about C/C++ the loose types, why do I now have to look up on Google/MSDN to remember

string value = ASCIIEncoding.ASCII.GetString(array);

when I could simply loop through the array and do something like this? Sorry, my memory with C/C++ is starting to be vague, I’m not even sure if it does give the right values, but the point is we could do something like this without having to memorize a bunch of other methods.

charVal[i] = byteVal[i];

Well, Javascript has that flexibility. I can use a variable without knowing what it is, I don’t need to unbox an object and get an InvalidCastException, I don’t need to convert the object to a string and then use TryParse to see if it worked.

With Javascript, it’s straight forward, I can even do this

var a = "12";
var b = "23";
var c = a * b; //c = 816
var d = a + b; //d = "1223"

Well, it’s not the best example, but at least it means I don’t need to do something like this

int selected = Convert.ToInt32(list.SelectedItem);

2) It’s simple.

Following the above point, we know exactly how to fix an issue because there is only so much details to javascript. On the other hand, with C# we’d probably have to put a try catch around Convert.To* method which always introduce scope issues and performance concerns when the code enters the catch block. Or we can look up on MSDN to find out about TryParse method.

With javascript, although I can’t find a good use case right now, the syntax will be simple. Most likely something like

if (isNaN(val))

3) It’s super extensible

People have been advocating design patterns with Java and C#, but I find it much easier to learn with Javascript, because with javascript, there is no Interface, there is no Delegate. You don’t have to worry about these constraints a framework imposes. You simply try to call the method, if it doesn’t exist, you implement it.

It makes learning the patterns much simpler because it just make sense.

In a way, I find javascript ENCOURAGES MV* like patterns because we get so tired of writing the same HTML code over and over again, it’s only nature to decide to add the method to prototype, or make it a jQuery plugin.

Yes, I’m bringing jQuery into this conversation. jQuery is proof why javascript is awesome not the other way around.

I haven’t seen any language that allows you to create a wrapper around an object to extend its functionalities as simply as jQuery does.  Sure “this” is confusing at times, but I’ve come to love it.

What I’ve seen people say about javascript and what I have to say about them

1) jQuery/NodeJS/Coffee Script etc… are 100X better.

I’ve seen people on the Internet saying there are so many frameworks that can do things 100X better, but to me. That’s EXACTLY what makes javascript AWESOME!

Its simplicity and extensibility is the reason why these libraries/frameworks can come into existence.

So, while people love using these new frameworks, I like to come back and think about what allows them to be so great.

2) You have to write a lot of ugly codes over and over again

Sure, DOM manipulation can get ugly, especially with the naming and classes, indexing. But that’s not a reason to dislike javascript, it’s these so-called weaknesses that encourages libraries like jQuery to come about.

Once prototype is understood, it only comes as a second nature to factor them away, and now there is jQuery, it’s as simple as wrapping this code around $.fn.name(){}

So maybe my view will change later as I work now more and more web oriented stuff at work, but so far, I like it.


Firefox Radio button issue – Selects the next option on refresh

This is something I encountered in my last job, don’t know if it still happens

But in case anyone encounters the same problem. Here is the gist of it

Suppose a page has a radio button with three options like so

(#) Option 1
( ) Option 2
( ) Option 3

Selection will change on every refresh, suppose we pressed F5 to refresh the page, it will now look like this

( ) Option 1
(#) Option 2
( )  Option 3

This behaviour is only present on firefox, supposedly it has been a problem since 2007 but noone bothered to fix it: Bugzilla link

The “fix” is simple

Turn off autocomplete feature

<form autocomplete="off">

Or through jQuery

$(document).ready(function() {
 if($.browser.mozilla) $("form").attr("autocomplete", "off");