Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > A Javascript array that can respond to changes

Reply
Thread Tools

A Javascript array that can respond to changes

 
 
Ben Katz
Guest
Posts: n/a
 
      09-29-2003
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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-29-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 - (E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      09-29-2003
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Can a DefaultCellEditor respond to custom events. JoeyB Java 4 07-25-2009 02:57 AM
How I can respond to client report from SSRS render without exposedthe SSRS address? mttc ASP .Net 0 07-20-2009 08:48 PM
DropdownLIst doest not respond to changes Cherry ASP .Net Web Controls 2 02-06-2004 08:03 AM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM



Advertisments