1

I've been learning OOP recently and have decided to take that paradigm shift into it (its not easy...at ALL) and in trying some of the concepts of it I'm having a bit of a problem inheriting from the Array class in ActionScript 3.0 (not that i have tried in AS 2.0)...sigh. Anyway I am trying to call the parent constructor to instantiate the ...rest arguments from within the child class like so

public class CustomArray extends Array{

    public function CustomArray(...rest):void {
        super(rest);
    }
}

And I keep getting this Error from the output...

ReferenceError: Error #1056: Cannot create property 0 on classes.CustomArray.

...to my utter dismay :(.

I'm obviously doing something wrong but for the love of me can't seem to find out what it is. Really in need of help. Thanks.

3 Answers 3

1

Unfortunately in AS3 you can't call super constructor and pass parameters to it in Function::apply style, so in your Array implementation array with length=1 and one element (the passed rest parameter with the type of Array) will always be created. If you want to implement the default AS3 Array constructor behavior:

Array Constructor
public function Array(... values)

Parameters
    ... values — A comma-separated list of one or more arbitrary values.

Note: If only a single numeric parameter is passed to the Array constructor, 
it is assumed to specify the array's length property.

You have to add some code to the constructor of your CustomArray:

public dynamic class CustomArray extends Array
{
    public function CustomArray(...rest)
    {
        super();

        var i:int, len:int;
        if(rest.length == 1)
        {
            len = rest[0];
            for (i = 0; i < len; i++) 
                this[i] = "";
        }
        else
        {
            len = rest.length;
            for (i = 0; i < len; i++) 
                this[i] = rest[i];
        }
    }
}

Also don't forget to make this class dynamic.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the added explanation.
1

Declare this class as dynamic. Also constructor is a method which doesn't specify return type, remove :void from its declaration.

Comments

1

Sorry to revive this "old" thread. I felt compelled to improve the constructor proposed by fsbmain.

When one calls new Array("foo"), an Array object is created containing the String "foo" - ["foo"]. So I think the custom constructor must take that possibility into account ( only one parameter that is not a number).

Here's the code I propose:

package {
    public dynamic class MyArray extends Array {
        public function MyArray(...rest) {

        //  super(); 

        // I think super() is not mandatory here since 
        // we are basically replacing the constructor...

            var i: int, len: int;
            if (rest.length == 1) {
                if(rest[0] is Number){  
                    len = rest[0];
                    for (i = 0; i < len; i++) {
                        this[i] = null; // empty values must be null, not ""
                    }
                }else{
                    this[0]=rest[0];
                }
            } else {
                len = rest.length;
                for (i = 0; i < len; i++) {
                    this[i] = rest[i];
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.