Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"".split(" ") in SO's javascript

Tags:

javascript

I've seen a lot of idioms, most of them clever and logical once explained. But while I was looking over SO's javascript to get some ideas about good interface design I saw the following line:

initTagRenderer("".split(" "), "".split(" "));

Which really threw me for a loop. Obviously, they initialize the tag renderer with two arrays containing exactly one null-string argument (or [""], which "".split(" ") evaluates to). That part I understand (having done the same thing in my own code). But it seems like passing a literal would accomplish the same thing.

Is there some non-obvious reason for doing this that I'm failing to see as a newcomer (to js, not programming)?

Also, I did try searching, and got lots of info on split() itself (which I already understand quite well), but not the idiom; Googling for double quotes is pretty fruitless.

Edit: It was the obvious answer. This part of the code is dynamically generated, and is not usually populated on SO proper.

like image 629
bkconrad Avatar asked Mar 18 '12 03:03

bkconrad


1 Answers

There is probably server-side code that dynamically populates those arguments to look something like this:

initTagRenderer("javascript php".split(" "), "ruby lisp".split(" "));

What that function does, I have no idea. But it must require an array of tag names, and it's easier to generate a space-delimited list rather than a JS array literal.

Edit

After some heroic investigating, it appears that initTagRenderer does indeed nicely format question tags. Tags that match a name in the first parameter are given a required-tag class, and tags that match the second parameter are given a moderator-tag class.

According to Madmartigan, it's used on meta:

initTagRenderer(
     "bug feature-request discussion support".split(" "),
     "faq status-completed status-declined status-bydesign status-norepro status-reproduced status-planned status-deferred status-review featured community-ads".split(" ")
);

View example


Relevant (but minified) line of full.js:

function initTagRenderer(f,c){window.tagRenderer||(window.tagRendererRaw=function(b,g){var g=g||"",e="";g||(f&&-1<$.inArray(b,f)?e=" required-tag":c&&-1<$.inArray(b,c)&&(e=" moderator-tag"));return"<a class='post-tag"+e+"' href='"+g+"/questions/tagged/"+encodeURIComponent(b)+"' title=\"show questions tagged '"+b+"'\" rel='tag'>"+b+"</a>"},window.tagRenderer=function(b,c){return $(tagRendererRaw(b,c))})}
like image 140
benesch Avatar answered Sep 26 '22 17:09

benesch