开发者

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?

开发者_如何学JAVA

Data 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:

  1. 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
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜