It’s JS Again

More things I hate about JS. Why you give a shit about this? Well, actually you don’t, but maybe together we can find better ways to solve stuff.

So we all know that there are no associate dictionaries in JS, and it’s really a hack of the Object ‘class’. I dare to use class here, bah. Anyway, say you are passed an object as a parameter and you want to know if it’s empty before you scan it. And say the only way is the most straight-forward one:

function f(x) {
var isEmpty = true;
for (var i in x) { isEmpty = false; break; }
return isEmpty;
}

You really have to iterate the items in order to find out if the dictionary is empty or not. Things like x == {}, didn’t work, but was worth trying. And you cannot access anything like children,nodes,child or whatever to see how to iterate the keys on your own.

If you know any shorter and correct way to do it, I would really like to hear it.

Now there’s thing ugliness with the values you put in the dictionary for example:

f({bla:0}) will call f with a dictionary that contains a key “bla” with a value of 0. But what if you add a line preceding that call with:
var bla = “something”;
f({bla:0})

Well, the people who really know JS well, or had fallen into this pit before will know that the dictionary will look the same as buffer. JS doesn’t care if you put any kind of quotes, if at all, surrouding the name of the key. Now if you want to pass a dictionary as a parameter inline, you must declare the whole dictionary before the call and pass it as a parameter.

var tmp = {}’
var bla = “something”;
tmp[bla] = 0;
f(tmp);

Another thing I really didn’t like about JS is that you can start a regexp out of the blue. /bla/.exec … Now stop and think about this. This is not PERL, which regexps are really part of the language. This is a ugly way to create a regexp, and to think that you get an object from that thing and you can execute it.

Now I see this thing often: var myRE = new RegExp(/bla/);

Which is a bit better, but then why you need the slashes to denote a regexp? You went that far for free. Sucker ;) But yes, it makes the code more readable, I agree to that.

Oh why, another so lovely thing happened to me today when I was using some SOAP library written in JS to send a request to my server, back at work. There was some function which tried to serialize the parameters you pass to it automagically without knowing the types into SOAP. Of course, as JS is a scripting language we can know the types of the parameters passed to us easily, right? That’s what I thought, until I saw that Safari doesn’t declare a constructor for its Array’s as some people expect it to (or as some other browsers do). The code to get the type of a parameter:

(/function\s+(\w*)\s*\(/ig).exec(o[p].constructor.toString());

Again, my favorite regexp out of the blues. Leave that aside. See the way it gets the constructor (yes objects apparently have those) and tries to get its string. Well, beat me why Safari returns an Object here where all others return Array (in my specific case). But kill me why this fugly hack and not an elegant safe:

instanceof (o[p]). toString();

Ok, I lied, this doesn’t really work, and I’ve wished it would. Unforetunately instanceof can be used only as a boolean operator kind of stuff. Therefoer,

if o[p] instanceof Array
if o[p] instanceof Object
and etc, date, string, whatever.

So maybe, there lies the answer it’s a piece of a few lines rather than one. But if you ask me, I would prefer latter.
One more catch, if you test instance of Object first, all types will return true to that one :)
Another point is that ‘new Array’  and ‘[]’ are of the same type…Strong types, nay.
I forgot to mention that typeof return ‘object’ almost for everything.

Overall, I really don’t understand how web-apps work. There are so many pits to fall into. It’s really amazing how the world work with Standards Suggestions! Now don’t get me started on CSS.

2 Responses to “It’s JS Again”

  1. Danny says:

    Which SOAP library did you use?

Leave a Reply