<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:
- Why in the first case, we can access
f
before define it? - What's the different between the two cases?