Q:

Interesting and strange behaviour of objects and closure in Javascript

See the code
<script type = 'text/javascript'>
function a()
{
    ;
}
a.prototype.hello = function()
{
    alert('hello');
}
(function()
    {
        var b = 8;
    }
());
</script>​

I'm not creating an object of a and not calling hello().But I'm getting the hello() called.
When I remove the closure ,the function is not automatically called.
ie. for
<script type = 'text/javascript'>
function a()
{
    ;
}
a.prototype.hello = function()
{
    alert('hello');
}
</script>

What is the reason for this strange behaviour?
http://jsfiddle.net/6yc9r/
and
http://jsfiddle.net/6yc9r/1/

A:

By omitting the semicolon, you are accidentally invoking the hello() function. This is why to use semicolons, even though the automatic semicolon insertion function of JS engines makes them seem like they're not necessary! Try this:
<script type = 'text/javascript'>
function a()
{
    ;
}
a.prototype.hello = function()
{
    alert('hello');
};
(function()
    {
        var b = 8;
    }
());
</script>​

A:

The reason is that you're missing a ;.
Because there is no semicolon between the function expression and the ( on the next line, the second function becomes a parameter to the first, like this:
a.prototype.hello = function()
{
    alert('hello');
}(function() { ... }());

