A Javascript array that can respond to changes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Katz

    A Javascript array that can respond to changes

    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("z ero", "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
  • Lasse Reichstein Nielsen

    #2
    Re: A Javascript array that can respond to changes

    ssheetz22@earth link.net (Ben Katz) writes:
    [color=blue]
    > 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?[/color]

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

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

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: A Javascript array that can respond to changes

      > Is it possible to have a JavaScript object that works just like a[color=blue]
      > 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("z ero", "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?[/color]

      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(ar ray, method) {
      this.get = function (index) {
      return array[index];
      };
      this.put = function (index, value) {
      array[index] = method ? method.apply(th is, [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");



      Comment

      Working...