{"id":64,"date":"2008-05-14T17:04:26","date_gmt":"2008-05-14T19:04:26","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=64"},"modified":"2008-05-14T17:04:26","modified_gmt":"2008-05-14T19:04:26","slug":"its-js-again","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=64","title":{"rendered":"It&#8217;s JS Again"},"content":{"rendered":"<p>More things I hate about JS. Why you give a shit about this? Well, actually you don&#8217;t, but maybe together we can find better ways to solve stuff.<\/p>\n<p>So we all know that there are no associate dictionaries in JS, and it&#8217;s really a hack of the Object &#8216;class&#8217;. I dare to use class here, bah. Anyway, say you are passed an object as a parameter and you want to know if it&#8217;s empty before you scan it. And say the only way is the most straight-forward one:<\/p>\n<p>function f(x) {<br \/>\n var isEmpty = true;<br \/>\n for (var i in x) { isEmpty = false; break; }<br \/>\n return isEmpty;<br \/>\n}<\/p>\n<p>You really have to iterate the items in order to find out if the dictionary is empty or not. Things like x == {}, didn&#8217;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.<\/p>\n<p>If you know any <em>shorter<\/em> and correct way to do it, I would really like to hear it.<\/p>\n<p>Now there&#8217;s thing ugliness with the values you put in the dictionary for example:<\/p>\n<p>f({bla:0}) will call f with a dictionary that contains a key &#8220;bla&#8221; with a value of 0. But what if you add a line preceding that call with:<br \/>\nvar bla = &#8220;something&#8221;;<br \/>\nf({bla:0})<\/p>\n<p>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&#8217;t care if you put any kind of quotes, if at all,\u00a0surrouding the name of the key. Now if you want to pass a dictionary as a parameter inline, you must declare\u00a0the whole dictionary\u00a0before the\u00a0call and pass it as a parameter.<\/p>\n<p>var tmp\u00a0= {}&#8217;<br \/>\nvar bla = &#8220;something&#8221;;<br \/>\ntmp[bla] = 0;<br \/>\nf(tmp);<\/p>\n<p>Another thing I really didn&#8217;t like about JS is that you can start a regexp out of the blue. \/bla\/.exec &#8230; Now stop and think about this. This is not PERL, which regexps are really <em>part<\/em> 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.<\/p>\n<p>Now I see this thing often: var myRE = new RegExp(\/bla\/);<\/p>\n<p>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.<\/p>\n<p>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 <em>scripting<\/em> language we can know the types of the parameters passed to us easily, right? That&#8217;s what I thought, until I saw that Safari doesn&#8217;t declare a constructor for its Array&#8217;s as some people expect it to (or as some other browsers do). The code to get the type of a parameter:<\/p>\n<p>(\/function\\s+(\\w*)\\s*\\(\/ig).exec(o[p].constructor.toString());<\/p>\n<p>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:<\/p>\n<p>instanceof (o[p]). toString();<\/p>\n<p>Ok, I lied, this doesn&#8217;t really work, and I&#8217;ve wished it\u00a0would. Unforetunately instanceof can be used only as a boolean operator kind of stuff. Therefoer,<\/p>\n<p>if o[p] instanceof Array<br \/>\nif o[p] instanceof Object<br \/>\nand etc, date, string, whatever.<\/p>\n<p>So maybe, there lies the answer it&#8217;s a piece of a few lines rather than one. But if you ask me, I would prefer\u00a0latter.<br \/>\nOne more catch, if you test instance of Object first, all types will return true to that one :)<br \/>\nAnother point is that &#8216;new Array&#8217;\u00a0 and &#8216;[]&#8217; are of the same type&#8230;Strong types, nay.<br \/>\nI forgot to mention that typeof return &#8216;object&#8217; almost for everything.<\/p>\n<p>Overall, I really don&#8217;t understand how web-apps work. There are so many\u00a0pits to fall into. It&#8217;s really amazing how\u00a0the world work with <strike>Standards <\/strike>Suggestions! Now don&#8217;t get me started on CSS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More things I hate about JS. Why you give a shit about this? Well, actually you don&#8217;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&#8217;s really a hack of the Object &#8216;class&#8217;. I dare to use class here, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-12","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/64"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}