Status of Array extras in ECMA 4

zwetan zwetan at gmail.com
Sat Sep 1 11:51:41 PDT 2007


(typed too fast and missed the redirection to ES4 list)

Hi,

[...]
>
>  However, according to the current ECMAScript 4 specification, these methods
> appear to be missing from Array.prototype:
>
> http://developer.mozilla.org/es4/spec/chapter_19_native_objects.html#array_objects
>

please check this link instead
http://wiki.ecmascript.org/doku.php?id=spec:chapter_19_native_objects

afaik,
the current AS3 implementation provide some (if not all) of the Array
"extra" methods
the ES4 milestone 0 provide also those Array "extra"

and if the Array class stays dynamic

nothing will prevent you to add the extras you would need

ex: reduce which appears in JS 1.8 is not there (yet)

Array2.as
(code from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduce
)
----
package extensions
    {

    public var Array2:Boolean = true;
    trace( "Array extensions loaded" );

    Array.prototype.reduce = function( fun:Function, ...args ):*
        {
        var len:uint = this.length;

        // no value to return if no initial value and an empty array
        if( len == 0 )
            {
            throw new TypeError();
            }

        var i:int = 0;
        if( args.length > 0 )
            {
            var rv:* = args[0];
            }
        else
            {
            do
                {
                if (i in this)
                    {
                    rv = this[i++];
                    break;
                    }

                // if array contains no values, no initial value to return
                if (++i >= len)
                    {
                    throw new TypeError();
                    }
                }
            while (true);
            }

        for( ; i < len; i++ )
            {
            if (i in this)
                {
                rv = fun.call(null, rv, this[i], i, this);
                }
            }

        return rv;
        }

    }
----

and use it like that

----
package
    {
    import flash.display.Sprite;

    public class sandbox3 extends Sprite
        {
        public function sandbox3()
            {
            import extensions.Array2;

            var total:* = [0, 1, 2, 3].reduce( function(a:*, b:*):*
{return a + b;} );
            trace( total );

            var flattened:* = [[0,1], [2,3], [4,5]].reduce(
function(a:*,b:*):* {return a.concat(b);}, [] );
            trace( flattened );

            }
        }

    }
----

just to say that in AS3 you can already add those extras
and in ES4 final you will still be able to do that and even more

zwetan



More information about the Es4-discuss mailing list