开发者

ActionScript Defining a Static Constant Array

is it not possible to define a static const array? i would like to have an optio开发者_StackOverflow社区nal parameter to a function that is an array of colors,

private static const DEFAULT_COLORS:Array = new Array(0x000000, 0xFFFFFF);

public function myConstructor(colorsArray:Array = DEFAULT_COLORS)
{
}

i know i can use ...args but i actually wanting to supply the constructor with 2 separate arrays as option arguments.


Not possible, but you could this to simulate this behaviour:

private static const DEFAULT_COLORS:Array = new Array(0x000000, 0xFFFFFF);

public function myConstructor(colorsArray:Array = null)
{
    colorsArray = colorsArray ? colorsArray : DEFAULT_COLORS;
}

This will not work if your function is coded in a way such that null could be a valid value (to signal some condition, for instance), but probably that's not the case here.

Edit

If you plan to write to colorsArray in myConstructor, it would be wise to make a copy of DEFAULT_COLORS here:

colorsArray = colorsArray ? colorsArray : DEFAULT_COLORS.slice();

The reference to the DEFAULT_COLORS Array is constant, but its contents are not, so you could accidentally change your default values.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜