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.
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.
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))})}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With