JavaScript variables can be used to hold two types of values: primitive type values and reference type values. Basic type values and reference type values have the following characteristics:
- The base type value occupies a fixed amount of space in memory and is therefore stored in the stack memory;
- Copying a base type value from one variable to another creates a copy of the value;
- The value of a reference type is an object that is stored in heap memory;
- A reference-type-worthy variable actually contains not the object itself, but a pointer to that object;
- Copying the value of a reference type from one variable to another is actually a pointer, so two variables end up pointing to the same object;
- Determine which base type a value can use the TypeOf operator, and determine which reference type can use the instanceof operator.
4.1.3 Passing parameters
1<script type= "Text/javascript" >2 functionAddten (num) {3num + = 10;4 returnnum;5 }6 7 varCount = 208 varresult =Addten (count);9alert (count);// -Tenalert (result);//Ten One A</script>
1 <script type= "Text/javascript" >2 function setName (obj) {3 Obj.name = "Nicholas"; 4 } 5 var New Object (); 6 setName (person); 7 alert (person.name); // "Nicholas" 8 </script>
4.2.2 does not have a block level scope
<script type= "Text/javascript" > function add (NUM1, num2) { var sum = num1 + return sum; var result = Add (10, 20); // 30 alert (sum); // because sum is not a valid variable, it causes the error </script>
1 <script type= "Text/javascript" > 2 function add (NUM1, num2) { 3 sum = num1 + num2; 4 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 varcolor = "Blue";3 4 functionGetColor () {5 varcolor = "Red";6 returncolor;7 }8 9Alert (GetColor ());//"Red"Ten</script>
JavaScript Advanced Programming (Third Edition) Chapter fourth variables, scope and memory issues