宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > AS2与AS3技术

[AS3]AS3.0中的函数定义与语句

2016-10-13 00:16:48 点击量:9 标签: 收藏本文

1、函数表达式定义法:必须先定义,才可以调用

  1. var a:Function = function(a:int, b:int):int{  
  2. return (a+b);  

2、函数语句定义法:优先自动加载,所以调用顺序不用考试

  1. function addnum(a:int, b:int):int{  
  2. return (a+b);  
  3. }   
  4. var c:int = addnum(2,3);  
  5. trace(c);//极酷播放器提示:5 

3、this对象的分析

  1. eg:  
  2. var num:int = 3;  
  3. function testThisA(){  
  4. trace (num);  
  5. trace (this.num);//牢牢指向当前域  
  6. trace (this);  
  7. }  
  8. var testThisB:Function = function testThisB(){  
  9. trace (num);  
  10. trace (this.num);//随着函数附着的对象不同,this指向的对象也会随之改变  
  11. trace (this);  
  12. }  
  13. var obj:Object = {num:300};  
  14. obj.testB = testThisB 
  15. obj.testA = testThisA 
  16. testThisA();  
  17. 3  
  18. 3  
  19. [object MainTimeline]  
  20. testThisB();  
  21. 3  
  22. 3  
  23. [object MainTimeline]  
  24. obj.testA();//函数语句定义法会牢牢指向当前域  
  25. 3  
  26. 3  
  27. [object MainTimeline]  
  28. obj.testB();//函数表达式定义法则会引用obj对象  
  29. 3  
  30. 300  
  31. [object Object]  
  32. testThisA.call(obj);//函数语句定义法会牢牢指向当前域  
  33. 3  
  34. 3  
  35. [object MainTimeline]  
  36. testThisB.apply(obj);//函数表达式定义法则会引用obj对象  
  37. 3  
  38. 300  
  39. [object Object] 

4、不变对象不会在函数中被改变,因为启用了新的对象,而数组等值引用类型的则被改变

  1. function replaces(a:int, b:Array):void{  
  2. a = 100;  
  3. b.push(100);  
  4. }  
  5.  
  6. var a:int =5;  
  7. var b:Array = [1,2,3];  
  8. replaces(a,b);  
  9. trace(a)//5  
  10. trace(b)//1,2,3,100 

5、函数设置默认参数,as3中参数在没有默认值的情况下必须对应,as2中可无视参数个数

  1. function test(a:int=1,b:int=2):void{  
  2. trace("参数长度:" + arguments.length);  
  3. trace(a+b);  
  4. if(a+b > 0)arguments.callee(a-1, b-1);//arguments.callee用于参数的递归  
  5. }  
  6. test();  
  7.  
  8. 结果:  
  9. 参数长度:0  
  10. 3  
  11. 参数长度:2  
  12. 1  
  13. 参数长度:2  
  14. -1 

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室