A Javascript array that can respond to changes

Discussion in 'Javascript' started by Ben Katz, Sep 29, 2003.

  1. Ben Katz

    Ben Katz Guest

    Is it possible to have a JavaScript object that works just like a
    standard Array, except that when the array is modified, a function
    gets called which can then do some processing on the array?

    Like this:
    // SpecialArray has a function called Notify
    function Notify()
    {
    // process the array with changes made
    }
    var myarray = new SpecialArray("zero", "one", "two", "three");
    myarray[1] = "ein"; // after this change is made, function "Notify"
    is called


    I know you can derive a new object from Array, but you cannot directly
    override the [] operator.

    Can you add a function or event handler to a regular Array object that
    gets called when the array changes?

    Thanks for any input.
    bk
     
    Ben Katz, Sep 29, 2003
    #1
    1. Advertising

  2. (Ben Katz) writes:

    > Is it possible to have a JavaScript object that works just like a
    > standard Array, except that when the array is modified, a function
    > gets called which can then do some processing on the array?


    Generally not.
    Netscape and Mozilla supports attaching handlers to properties that
    are called when the properties change.
    <URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/object.html#1193628>

    Neither IE nor Opera have inplemented this feature, and it is not part
    of the ECMAScript standard.

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 29, 2003
    #2
    1. Advertising

  3. > Is it possible to have a JavaScript object that works just like a
    > standard Array, except that when the array is modified, a function
    > gets called which can then do some processing on the array?
    >
    > Like this:
    > // SpecialArray has a function called Notify
    > function Notify()
    > {
    > // process the array with changes made
    > }
    > var myarray = new SpecialArray("zero", "one", "two", "three");
    > myarray[1] = "ein"; // after this change is made, function "Notify"
    > is called
    >
    >
    > I know you can derive a new object from Array, but you cannot directly
    > override the [] operator.
    >
    > Can you add a function or event handler to a regular Array object that
    > gets called when the array changes?


    No, put you can define a method that will have the effect you want, with a
    different look perhaps. Make a constructor that takes two arguments: an array of
    values, and an optional method to be called when changed.

    function SpecialArray(array, method) {
    this.get = function (index) {
    return array[index];
    };
    this.put = function (index, value) {
    array[index] = method ? method.apply(this, [index, this.get(index),
    value]) : value;
    };
    }

    The method you supply takes three parameters: The index, the old value, and the
    new value. It should return the definitive new value (or the old value to leave
    it effectively unchanged). This method can also do any notifications that you
    need.

    myarray = new SpecialArray(["zero", "one", "two", "three"], function (index,
    older, newer) {
    return newer;
    });

    Use myarray's privileged .get(index) and .put(index, value) methods to read and
    write its contents.

    myarray.set(1, "eine");

    http://www.crockford.com/javacript/private.html
     
    Douglas Crockford, Sep 29, 2003
    #3
    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. Mickey Segal
    Replies:
    0
    Views:
    903
    Mickey Segal
    Feb 2, 2004
  2. mttc
    Replies:
    0
    Views:
    388
  3. JoeyB
    Replies:
    4
    Views:
    649
    John B. Matthews
    Jul 25, 2009
  4. Cherry

    DropdownLIst doest not respond to changes

    Cherry, Feb 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    100
    Marshal Antony
    Feb 6, 2004
  5. Jack
    Replies:
    9
    Views:
    197
Loading...

Share This Page