Vishal
Vishal

Reputation: 20617

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 it uses the default value of startIndex?

Upvotes: 1

Views: 200

Answers (3)

PatrickS
PatrickS

Reputation: 9572

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)

Upvotes: 1

Gunslinger47
Gunslinger47

Reputation: 7061

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.

Upvotes: 1

Juan Pablo Califano
Juan Pablo Califano

Reputation: 12333

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

startIndex();

Upvotes: 0

Related Questions