局部变量和全局变量的作用域的首次理解

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Webville Tunes</title>
</head>
<body>
	<script>
		var avatar = "genertic";
		var skill = 1.0;
		var pointsPerLevel = 1000;
		var userPoints = 2008;
		function getAvatar(points) {
			var level = points / pointsPerLevel;
			 if (level == 0) {
			 	return "Teddy bear";
			 }
			 else if (level == 1) {
			 	return "Cat";
			 } else if (level >=2) {
			 	return "Gorilla";
			 }
		}
		function updatePoints(bonus,newPoints) {
			for (var i = 0;i < bonus; i++) {
				newPoints += skill * bonus;
			}
			return newPoints + userPoints;
		}
	</script>
</body>
</html>
在函数外的是全局变量,在函数内的是局部变量

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页