开发者

How do I call this function so that it uses the default parameter value?

slice() method

AS3 function slice(startIndex:Number = 0, endIndex:Number = 0x7fffffff):String

String:slice() doc

The documentation says the default value of startIndex is 0, How do I call this function so that i开发者_开发知识库t uses the default value of startIndex?


In AS3 , if a function requires parameters and some of the parameters have a default value, not passing any parameters will logically default to the parameters default value, as in Juan Pablo Califano's example.

If more than one parameter is needed and you wish to retain the first parameter default value, you will have to actually specify it , in order to set other parameters.

var str:String;
str.slice( 0 , 5 );
str.slice( 0 , 2 );

Take the Matrix class , for instance:

Matrix(a:Number = 1, b:Number = 0, c:Number = 0, 
              d:Number = 1, tx:Number = 0, ty:Number = 0)

If you needed to set the ty parameter by passing it as an argument, you would need to set every single parameter before it,

var matrix:Matrix = new Matrix(a:Number = 1, b:Number = 0, c:Number = 0, 
              d:Number = 1, tx:Number = 0, ty:Number = 50)

on the other hand, you could set a & b and leave the default values for the remaining parameters.

var matrix:Matrix = new Matrix(a:Number = 100, b:Number = 20)


Once you specify a parameter's values, you'll have to specify every parameter to the left of it. So if you want to leave out startIndex, you will have to leave out endIndex.

In some languages you could say str.slice(, 5) but not in AS3. To use the default value for startIndex you'll have to say str.slice() which creates a full copy of the String.

There doesn't seem to be much point in doing this for String since it's immutable. You see slice() more often on Array where it's a easy way to get a copy.


Just don't pass the argument and it will use the default one:

startIndex();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜