Javascript function in external file is undefined when declared in a certain way

Posted by Obay on Stack Overflow See other posts from Stack Overflow or by Obay
Published on 2010-04-16T07:28:33Z Indexed on 2010/04/16 7:33 UTC
Read the original article Hit count: 431

Filed under:
|

myfunc() runs successfully when called from within the same js file. but it is undefined (Firebug) when called from an HTML page:

JS file:

$(function() {
    myfunc() {
        alert('inside myfunc');
    }
    alert('outside myfunc');
    myfunc(); //this successfully runs myfunc()
});

HTML:

<script>
$(function() {
    myfunc(); //this doesn't run myfunc(). It's undefined
});
</script>

But when I change myfunc() declaration to:

myfunc = function () { ... }

It's no longer undefined, and runs successfully.

Sorry for this very noob question, but what just happened? Why did it work when I changed the way I declared the function?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript