Search code examples
javascriptanonymous-function

I have an anonymous function without parentheses, how does this work?


New to javascript,but i know that form

<script>(function(){ somecode })();</script> 

will immediately run somecode when it be interpreted.But when i was reading a html5 game source code,I encountered some code which form like this:

<script>(function(){})</script>

There is no parentheses attached.So what does it mean?

source code:https://github.com/oxyflour/STGame and the index.html has code form like below:

<script>(function(){})</script>

Solution

  • Refer the game.js file, they are using it as nodes that don't do anything(@Shilly) and accessing them with id in the script tag. I don't know what is being done with the d object but certainly it is being called somewhere, look how they're using the innerHTML

    else if (v.tagName == 'SCRIPT' && $attr(v, 'type') == 'text/html') {
      d[v.id] = v.innerHTML;
    } else if (v.tagName == 'SCRIPT') {
      d[v.id] = eval(v.innerHTML)(_t);
    }
    

    An example of what's being done:

    eval(document.getElementById('myscript').innerHTML)('test');
    <script id="myscript">
    (function(a) {
      a ? console.log(a) : console.log('some string')
    })
    </script>