Flex函数一时糊涂


  今天做项目,需要把itemRenderer里的data对象传递给外部的事件函数,原先以为事件处理函数传参只能传event,而忘记了函数的本质。传什么参数是根据函数体里规定好的。因为调用该函数的位置是处于itemRenderer内部,所以可以把data对象传递给该函数。  height="400" title="端口略称规则管理界面"> height="5%" horizontalAlign="center">       height="5%">   height="90%" id="portAliasTable">                  外部事件监听函数是这样,
  public function modifyPortAlias_clickHandler(data:Object):void{
  }
  如果外部的监听函数public function modifyPortAlias_clickHandler(event:MouseEvent,data:Object):void{
  }
  那么里面的是这样click="outerDocument.modifyPortAlias_clickHandler( event,data)"
  既然遇到了函数,就讲讲Flex中的函数,Function是flex中的内置的复杂数据类型,函数是可在 ActionScript 中调用的基本代码单位。ActionScript 中用户定义的函数和内置函数都由 Function 对象来表示,该对象是 Function 类的实例。
  我们平常见到的定义函数public function doXXX():void{}等价于var doXXX:Function = function():void{}.
  所以可以在一个函数中嵌套一个函数,举例: public function eatOrange(e:Event):void{ function confirmHandler(dlg_obj: CloseEvent):void{ if(dlg_obj.detail == Alert.YES){ _fruitsColl.removeItemAt(fruitCB.selectedIndex); _fruitsColl.refresh(); trace(_fruits.toString()); } } var confirmDlg: Object = Alert.show("一经删除无法恢复! 您确认要删除吗?", "确认",Alert.YES|Alert.NO, null, confirmHandler, null, Alert.YES); } 但是这样写不太好理解,所以也可以这样。 public function eatOrange(e:Event):void{ var confirmHandler: Function = function(dlg_obj: CloseEvent):void{ if(dlg_obj.detail == Alert.YES){ _fruitsColl.removeItemAt(fruitCB.selectedIndex); _fruitsColl.refresh(); trace(_fruits.toString()); } } var confirmDlg: Object = Alert.show("一经删除无法恢复! 您确认要删除吗?", "确认",Alert.YES|Alert.NO, null, confirmHandler, null, Alert.YES); } 都能正常运行。
  在声明方法时直接对参数进行赋值,赋予默认值或null值。这样调用方法时不用每次都进行传值。典型的例子,可以看Alert.show()方法。
  还可以在变量名前使用..标记定义可选参数以及不定数量的参数。该变量将包含一个数组参数,可被循环遍历处理。
  例如:public function unlimitedArgumentsFunction(..arguments):void{
  for each(var arg:Object in arguments){
  }
  }

猜你喜欢

转载自aat36aat.iteye.com/blog/1574155