代码示例:(标识:eg_js_function_scope_2)
<!DOCTYPE html>
<html>
<body>

<p>函数可以访问函数外部定义的变量:</p>

<button type="button" onclick="myFunction()">单击这里!</button>

<p id="demo"></p>

<script>
var a = 4;
function myFunction() {
  document.getElementById("demo").innerHTML = a * a;
} 
</script>

</body>
</html>
运行结果: