Javascript 变量补充篇

      前面有一篇日志是写道有关JS变量的,这里还写JS变量。看例子:

<script type="text/javascript">
		var str='abcde';   
		var obj=new String(str);
	
		function newToString(){
			return 'Hello world!';
		}
		function func(val){
			val.toString=newToString;
		}
		//示例1:传入值
		func(str);
		alert(str);
		//示例2:传入引用
		func(obj);
		alert(obj);
	</script>
Tips:func(xxx);是修改值的。

      这样如果难看一点,可以看看之前的那个差不多的例子:

var a = 3.14;  // Declare and initialize a variable
var b = a;     // Copy the variable's value to a new variable
a = 4;         // Modify the value of the original variable
alert(b)       // Displays 3.14; the copy has not changed

var a = [1,2,3];  // Initialize a variable to refer to an array
var b = a;        // Copy that reference into a new variable
a[0] = 99;        // Modify the array using the original reference
alert(b);         // Display the changed array [99,2,3] using the new reference

     变量声明

     变量声明有两种方法:显式和隐式。

     显式一般是指使用Var声明,隐式(即用即声明)

    

//声明变量
var str='abc';
//声明函数
function foo(){
  str='abc';
}
//南明异常对象ex
try{
  //...
}
catch(ex){
  //...
}
原文地址:https://www.cnblogs.com/coolicer/p/1848768.html