I Love China

技术·人生

技术服务生活
繁體
5月 19th, 2007

关于函数参数

关于函数参数
如果传递的参数是Object类型的,那么参数将以引用的形式传递,否则传递的只是一个拷贝。注意Array类型也是Object类型。
例子:(数组作为参数)

ActionScript Code:
  1. var testAry:Array = [1,4,0,5,3];
  2. function changeAry(ary:Array)
  3. {
  4.     ary[0] = 6;
  5. }
  6. trace("函数调用前内容是 " + testAry[0])
  7. changeAry(testAry);
  8. trace("函数调用后内容是 " + testAry[0])

输出结果是

Output Code:
  1. 函数调用前内容是 1
  2. 函数调用后内容是 6

Object最为参数:

ActionScript Code:
  1. var testAry:Array = [1,4,0,5,3];
  2. var testObj:Object = new Object()
  3. testObj.x = 10;
  4. function changeObj(obj:Object)
  5. {
  6.     obj.x = 20
  7. }
  8. trace("函数调用前内容是 " + testObj.x)
  9. changeObj(testObj)
  10. trace("函数调用后内容是 " + testObj.x)

输出结果是:

Output Code:
  1. 函数调用前内容是 10
  2. 函数调用后内容是 20

字符串作为参数:

ActionScript Code:
  1. var testStr:String = "Hello World!";
  2. function changeStr(str:String)
  3. {
  4.     str = "Good Bye!"
  5. }
  6. trace("函数调用前内容是 " + testStr)
  7. changeStr(testStr)
  8. trace("函数调用后内容是 " + testStr)

输出结果是

Output Code:
  1. 函数调用前内容是 Hello World!
  2. 函数调用后内容是 Hello World!

随机文章:

Leave a Reply