<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Javascript中的变量作用域事例</title>
<h2><u>Computer monitor specifications</u></h2>
<script language="javascript">
<!--
//定义全局变量
color="green";
var size=15;
function monitorSpecs()
{
color="purple";//未作定义,给全局变量赋值
price="$300.00";//在函数内生成全局变量(无var关键字)
var size=17;//只有在函数内定义(用var关键字)的此变量为局部变量
document.write("The ",size," inch ",color);
document.write(" monitor is ",price);
}
monitorSpecs();
document.write("<br> The ",size," inch ",color);
document.write(" monitor is ",price);
//全局变量与局部变量同名时,在函数内是局部变量,函数外是全局变量
//->
</script>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Javascript中的变量作用域事例</title>
<h2><u>Computer monitor specifications</u></h2>
<script language="javascript">
<!--
//定义全局变量
color="green";
var size=15;
function monitorSpecs()
{
color="purple";//未作定义,给全局变量赋值
price="$300.00";//在函数内生成全局变量(无var关键字)
var size=17;//只有在函数内定义(用var关键字)的此变量为局部变量
document.write("The ",size," inch ",color);
document.write(" monitor is ",price);
}
monitorSpecs();
document.write("<br> The ",size," inch ",color);
document.write(" monitor is ",price);
//全局变量与局部变量同名时,在函数内是局部变量,函数外是全局变量
//->
</script>
</head>
<body>
</body>
</html>