• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to extend system classes

Contributor ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

Hi,

just an example: I am using lots of arrays in my code, and so repeat the stanza

somearray[somearray.length-1]

all over the place.

Now, I could do something like

public dynamic class MyArray extends Array

{    public function get last():*

     {    return this[this.length-1];

     }

}

and replace all instances of new Array() by new MyArray().

This would allow to use

somearray.last

However, this would lose the shortcut [], and the "last" accessor would not be available on arrays set up by system code (say as part of data

collections)

There are similar examples for static functions that I would like to add to some classes. I could, of course, collect them into a class MathUtil or 'GeomUtil,

but I would prefer to relate them to, say Point or Vector3D

I have seen "prototype chain" etc mentioned, would that help?

TOPICS
ActionScript

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2012 Apr 28, 2012

Copy link to clipboard

Copied

create a static method:

package {

   

    public class A {

        public static function last(a:Array):*{

            return a[a.length-1];

        }

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 29, 2012 Apr 29, 2012

Copy link to clipboard

Copied

Hi,

well that would give me

A.last(myarray)

instead of the desired

myarray.last

Probably even that makes code somewhat more readable

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 29, 2012 Apr 29, 2012

Copy link to clipboard

Copied

>>However, this would lose the shortcut [], and the "last" accessor would not be available on arrays set up by system code (say as part of data collections)

I would say not an issue to both of those. When do you really use []? I never use it... and who cares about arrays set up by system code? This solves your issue... but while I don't see your first solution as a problem, I would use kglads way - and do actually. I have an array utilities class and make use of it exactly like that: A.last(myarray) and I do think it makes the code more readable as well. Just my .02

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 29, 2012 Apr 29, 2012

Copy link to clipboard

Copied

LATEST

Hi,

well, I really prefer [] over new Array() and {} over new Object() ....

I have one class that is declared

public dynamic class something extends Array

For the moment I have added

public function get lastsomething():*

to it. I found that telling plain arrays and this special construct apart (in code just a few weeks old) was not that easy, so I will probably use distinctive member names more often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines