Is it ever preferred to not declare the data type of the return value of a function?
Is it ever preferred to not declare the data type of the return value of a function?
开发者_如何学JAVAData type of return value is not declared:
function information(){
var myName:String="Mickey Mouse";
return myName;
}
trace(information());
Data type of return value is declared:
function information():String{
var myName:String="Mickey Mouse";
return myName;
}
trace(information());
Flex SDK compiler will consider the lack of return type as an error, so I guess this is pretty important :) If you need to have a function that returns several types of data:
- You are doing something wroong OOPly speaking (unfortunately you can't write different versions of the same function as you would do in Java), use a mother class or an interface to type correctly the returned data
- You may use
:*
If you're programming along the lines of the ECMAScript variant of AS3, then it's completely unnecessary to specify a return type. The programmer should have a good idea of what sort of variable is being returned, and specifying the return type is "just a convenience".
Unfortunately, many of the core classes have been locked down using the final
keyword so that their prototypes aren't editable, nullifying many of the advantages that ECMAScript provides.
精彩评论