标签:style blog color io 使用 ar java sp div
JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值。基本类型值和引用类型值具有以下特点:
1 <script type="text/javascript"> 2 function addTen(num) { 3 num += 10; 4 return num; 5 } 6 7 var count = 20 8 var result = addTen(count); 9 alert(count); //20 10 alert(result); //10 11 12 </script>
1 <script type="text/javascript"> 2 function setName(obj) { 3 obj.name = "Nicholas"; 4 } 5 var person = new Object(); 6 setName(person); 7 alert(person.name); //"Nicholas" 8 </script>
<script type="text/javascript"> function add(num1, num2) { var sum = num1 + num2; return sum; } var result = add(10, 20); //30 alert(sum); // 由于sum不是有效的变量,因此会导致错误 </script>
1 <script type="text/javascript"> 2 function add(num1, num2) { 3 sum = num1 + num2; 4 return sum; 5 } 6 var result = add(10, 20); //30 7 alert(sum); //30 8 </script>
1 <script type="text/javascript"> 2 var color = "blue"; 3 4 function getColor(){ 5 return color; 6 } 7 8 alert(getColor()); //"blue" 9 </script>
1 <script type="text/javascript"> 2 var color = "blue"; 3 4 function getColor(){ 5 var color = "red"; 6 return color; 7 } 8 9 alert(getColor()); //"red" 10 </script>
JavaScript高级程序设计(第三版)第四章 变量,作用域和内存问题
标签:style blog color io 使用 ar java sp div
原文地址:http://www.cnblogs.com/fashion1993/p/4017791.html