0
<html>
    <head>
        <script type="text/javascript">
            f();
            function f() {
                alert('in f()');
            }
        </script>
    </head>
</html>

This block of javascript will work. But the following cannot:

<html>
    <head>
        <script type="text/javascript">
            f();
            f = function() {
                alert('in f()');
            }
        </script>
    </head>
</html>

With the following error from Chrome:

Uncaught ReferenceError: f is not defined

My questions are:

  1. Why in the first case, we can access f before define it?
  2. What's the different between the two cases?
Trista
  • 57
  • 6

0 Answers0