JavaScript variable scope in Google Chrome
I have the following code:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<p id="p"></p>
<script>
var top = 9;
( function () { top += 1; document.getElementById( "p" ).innerHTML = top; } )()
</script>
</body>
</html>
It works fine in Internet Explorer (fingerprints 10), but in Google Chrome it prints [object window]. But if I change it like this:
( function () { var top = 9; top += 1; document.getElementById( "p" ).innerHTML = top; } )()
it works in Chrome too. The only change is that the variable is now local. Does this mean that in Chrome I cannot access the global variables? No matter what I do, I cannot get the correct result.
+3
source to share