Title: What part of JavaScript syntax is involved when you encounter (code)()?

I recently encountered a short piece of script for the BASE tag but it was in the form: (code)()

By this I mean it was a few lines of script, enclosed in parentheses, and followed by an empty set of parentheses.

<!--[if IE]><script type="text/javascript">  
    // Fix for IE ignoring relative base tags.
    (function() {
        var baseTag = document.getElementsByTagName('base')[0];
        baseTag.href = baseTag.href;
    })(); </script><![endif]-->

per Richard Connamacher at stack overflow at this link

I'm not sure how to research why the parentheses, in particular the second set, are there. Is it enclosure?

What is wrong with this question? I expect that given I don't know what I was looking at and how to describe it, there are issues. It is also difficult to search for. It could even be a syntax error but I doubt it.

I am certain the question is not phrased correctly. Would it be a mistake to even ask? More specifically, would it get blocked or down voted?

  • 7
    Please consider using a quote block or similar to help us understand where the original question ends and where your meta question begins. I'm consistently failing to parse it even though the night's still young. – Frédéric Hamidi Jul 19 '13 at 21:11
  • 2
    I think I extracted and quoted the question you're asking about, which should make this a little easier to parse. – Brad Larson Jul 19 '13 at 21:28
  • Brad, quoting the original question was much better than a horizontal line. Thanks. – DHorse Jul 19 '13 at 23:43
  • It's an immediately invoked function expression (IIFE) - not sure how best to ask the question, though – Adam Rackis Jul 19 '13 at 23:46
  • @GeorgeCummins I have added the code. Sorry, it took forever to find the post again. I suspect there is larger issue of how I express the questions rather than what the question actually is. – DHorse Jul 19 '13 at 23:48
  • 1
    @DHorse - sure thing. The first set of parenthesis turns the function declaration into a function expression, and the second set invokes said function expression: immediately invoked, function expression. Note that (function(){ /* code here */ }()) will also work – Adam Rackis Jul 19 '13 at 23:51
  • AdamRackis Well I didn't expect an actual answer but thanks, never heard of "an immediately invoked function expression." I now see there are some much more sophisticate scope issues I need to look at. There you go. In that light the question seems even more poorly worded. – DHorse Jul 20 '13 at 0:00

I was told on another thread that the questions was OK as is including the revisions made so credit ti frederic-hamidi and brad-larson

Additional credit to adam-rackis who answered the question rather than improving it.

Additionally credit to george-cummins who more or less pointed out to not be hypothetical. Meaning, include the actual code samples here or it is hard to know what might be wrong with the question.

OOPS, I still can't mark the question as answered. Odd. (see:Mark a comment as answer to a question)

  • Thank you for crediting us. Re: your last sentence, see Why must I wait 2 days before accepting my own answer?. – Frédéric Hamidi Jul 21 '13 at 17:09
  • @FrédéricHamidi Certainly, thank you, your above post was informative. One or more of you guys should move your comments into an answer so I can credit it properly (given the type of question this is.) – DHorse Jul 21 '13 at 17:25
  • With this type of question, I'm really not sure if I should repeatedly refine (edit) my original post. It is what I am doing. Of course, the final, postable version would be quite different from the original question asked. I can think of some alternate strategies. – DHorse Jul 21 '13 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .