Extending Array?

Discussion in 'Javascript' started by optimistx, Oct 7, 2009.

  1. optimistx

    optimistx Guest

    Some people recommend not to add methods and properties to objects like
    Array. The reason being that the names of those might collide with other
    programs on the same page made by others.

    The exception to this seems to be methods, which are included in the newer
    versions of javascript: they might be coded if not existing already.

    How would I then extend an Array? Define my own MyArray in my own namespace,
    and use Array as a prototype or ancestor for it? Then use it like

    myarray1 = new MyArray (...);
    myarray2 = new MyArray (...);
    myarray1.nicenewmethod();

    or something else: simple, elegant, educational, intuitive, robust,
    efficient, memoryefficient,
    programmerfriendly, socially polished, expertlike, artistic, beautiful, ...
    ?

    Example of such a code?
    optimistx, Oct 7, 2009
    #1
    1. Advertising

  2. On Oct 7, 11:11 am, optimistx wrote:
    > Some people recommend not to add methods and properties to
    > objects like Array.


    More likely it is not adding methods/properties to the -
    Array.prototype - that being recommended against.

    > The reason being that the names of those
    > might collide with other programs on the same page made by
    > others.


    That probably isn't the main reason for recommending not modifying the
    array's prototype. More the issue relates to the use of for-in to
    iterate over the enumerable properties of objects, where any
    properties added will enumerable, and so while you can know with
    certainty whether your code used for-in or not you cannot be certain
    that third party code will or will not. Thus there are circumstances
    where modifying the built-in prototypes may provoke issues.

    > The exception to this seems to be methods, which are included in
    > the newer versions of javascript: they might be coded if not
    > existing already.


    Adding those methods will result in enumerable properties, and so will
    provoke the/any issues that are of relevance.

    > How would I then extend an Array?


    Prototype based inheritance does not work with Arrays because arrays
    employ a specialised internal [[Put]] in order to react to their
    significant 'array index' and - length - property names. This internal
    [[Put]] method is not inherited by any object for which an array
    object appears either as its [[Prototype]] or on its prototype chain.
    Thus any object that inherits from an array on its prototype chain may
    gain all the methods of an array (and most array methods are specified
    as working with any object type) but it will not gain the special
    qualities that make an object an Array.

    The equivalent of 'extending'/'sub-classing' and Array is usually done
    by having a special method/function that creates ordinary arrays and
    directly modifies those, adding any methods by assigning references to
    functions to the created array's prpeties. That way the new object
    retains the Array's special [[Put]] method and continues to inherit
    the array methods, but also has any new properties/method needed.
    Obviously the added methods/properties of these modified array objects
    are enumerable, but that is true of the programmer-defined methods/
    properties of any 'class' in javascript.

    > Define my own MyArray in my own namespace,


    What do you imagine "namespace" means here?

    > and use Array as a prototype or ancestor for it? Then use it like
    >
    > myarray1 = new MyArray (...);
    > myarray2 = new MyArray (...);
    > myarray1.nicenewmethod();
    >
    > or something else: simple, elegant, educational, intuitive,
    > robust,
    > efficient, memoryefficient,
    > programmerfriendly, socially polished, expertlike, artistic,
    > beautiful, ...
    > ?
    >
    > Example of such a code?


    Archives?

    Richard.
    Richard Cornford, Oct 7, 2009
    #2
    1. Advertising

  3. optimistx

    optimistx Guest

    Richard Cornford wrote:

    >> Define my own MyArray in my own namespace,

    >
    > What do you imagine "namespace" means here?


    var MYINITIALS = MYINITIALS || {}

    where MYINITIALS may be e.g. SES or backwards internet address owned by me .

    Thus only one global variable is defined directly by me.

    ___

    If I understood your thorough and kind (thank you!) reply right it is good
    style to write e.g.

    var a = new Array(1,2,3,4);
    (or even better:
    var a = [1,2,3,4];
    }

    and define new methods and properties to a like this:

    a.newmethod = f; // f is a function definition or function expression
    a.newproperty = p; //

    but this could produce issues:

    Array.prototype.newmethod = f;
    Array.prototype.newproperty =p;

    [1,2,3,4].newmethod();
    [1,2,3,4].p = 'interesting_value';

    ?

    That actually answers my problem practically for almost all situations. How
    much is the in memory usage?

    I tried to test with Firefox 3.0.13 and Chrome in windows xp and task
    manager's memory display.The results were mixed and confusing.

    When generating one million very simple almost empty
    objects Firefox seemed to reserve about 200 bytes for each. When a simple
    method was
    added to each object (not their prototype)the same method for each, the
    memory usage increased with about 70 bytes each. I was expecting much
    smaller value, the size of a memory pointer (8-10 bytes?).

    The memory usage of Chrome was so small that I must have made some error in
    the setup.

    To define one method in the prototype instead of each generated object would
    anyhow add a pointer (or similar?reference) to each object. Or not? Is the
    difference in reality around 8-10 bytes per object? If so, and when memory
    space is measured in gigabytes nowadays, it is
    ridiculous to even think of saving some bytes, even kilobytes every now and
    then. 'Premature optimization' ?...

    Better method for measuring memory usage?
    optimistx, Oct 8, 2009
    #3
  4. optimistx

    VK Guest

    > How would I then extend an Array? Define my own MyArray in my own namespace,
    > and use Array as a prototype or ancestor for it? Then use it like
    >
    > myarray1 = new MyArray (...);
    > myarray2 = new MyArray (...);
    > myarray1.nicenewmethod();
    >
    > or something else: simple, elegant, educational, intuitive, robust,
    > efficient, memoryefficient,
    > programmerfriendly, socially polished, expertlike, artistic, beautiful, ...
    > ?


    <script type="text/javascript">

    function SuperArray() {
    this.method1 = arguments.callee.xMethod;
    }
    SuperArray.xMethod = function() {
    window.alert(
    'SuperArray says: ' + this
    );
    }

    function SuperDuperArray() {
    SuperArray.call(this);
    this.method2 = arguments.callee.xMethod;
    }
    SuperDuperArray.xMethod = function() {
    window.alert(
    'SuperDuperArray says: ' + this
    );
    }

    function SuperDuperMuperArray() {
    var arr = arguments[0] || [1,2,3];
    SuperDuperArray.call(arr);
    arr.method3 = arguments.callee.xMethod;
    return arr;
    }
    SuperDuperMuperArray.xMethod = function() {
    window.alert(
    'SuperDuperMuperArray says: ' + this
    );
    }

    var a = new SuperDuperMuperArray([1,2,3]);
    a.method1();
    a.method2();
    a.method3();

    var b = new SuperDuperMuperArray();
    b.method1();
    b.method2();
    b.method3();

    </script>
    VK, Oct 8, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joachim Dahl

    extending the builtin array class from C

    Joachim Dahl, Aug 14, 2004, in forum: Python
    Replies:
    0
    Views:
    269
    Joachim Dahl
    Aug 14, 2004
  2. Janwillem

    extending python with array functions

    Janwillem, Feb 4, 2008, in forum: Python
    Replies:
    4
    Views:
    284
    Janwillem
    Feb 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    486
    David RF
    Sep 3, 2009
  4. cerr
    Replies:
    6
    Views:
    258
    ImpalerCore
    Mar 17, 2011
  5. Oliver Saunders

    Extending Array and assigning to self

    Oliver Saunders, May 6, 2008, in forum: Ruby
    Replies:
    4
    Views:
    137
    Oliver Saunders
    May 6, 2008
Loading...

Share This Page