How do you declare a parameter that can be arbitory data type in actionscript 3?
Though this works:
开发者_开发百科 public function func(settings)
{
}
It's reporting a warning,so what's the standard way to do this?
You can use * or Object :
public function foo(bar:*):void{
}
public function foo(bar:Object):void{
}
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/specialTypes.html#* http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Object.html
Typing it to "*" or Object datatypes will work - but it's really not ideal. There is probably SOME way to specify a correct datatype.
One answer is to type the input to the most specific common ancestor of all possible inputs - so, for instance, if you know it's going to be a DisplayObject but sometimes it's going to be a Loader and sometimes it's going to be a Sprite, just type it to DisplayObject since both Loader and Sprite ARE technically DisplayObjects.
The other way is perhaps a bit more "correct", and that is to type it to an Interface. Your objects can implement that Interface, and then you're all set.
If you use "*" or Object then you are giving up all ability to debug at that point. Ideally you want to know exactly what objects exist and where they exist in the lifecycle of your application. Every time you cast something as an Object you're giving up - you're saying "And then it goes into this tunnel and disappears", so to speak. It's something best avoided, especially for larger projects that will have to be maintained by other people.
you should definitly specify the return type and parameter classes. this makes your code faster, and much much easier to read in 6 months!
So this would be:
public function saveSettings(settings:Array):void
{
}
or what ever might be the appropriate things to call your function :) But don't just call it func.. name it properly :)
精彩评论