473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Array object responding to changes

Is it possible to have an Array object (or an object derived from
Array) that is 'aware' of other code modifying its contents? I'd like
to have such an "onModify" function process an array everytime the []
operator is used to make a change. I know you can derive from Array,
but you cannot directly override the [] operator. Any way to make
this possible?

// possible definition of 'SpecialArray'
...
function OnModify()
{
// process the list
}

...

// an example of use
var myarray = new SpecialArray("zero", "one", "two", "three");
myarray[1] = "ein"; // this causes the array's OnModify to be
called
Thanks for any ideas.
bk
Jul 20 '05 #1
1 1749
> 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
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben Katz | last post by:
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? ...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
4
by: Rich | last post by:
Hello, I create multiple pictureboxe controls on a form in a For Loop and display thumbnail pictures. I need to add a Click event to these pictureboxes. Here is the routine that creates the...
2
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for...
3
by: Ron | last post by:
Hi, If it's possible, a quick example would REALLY help. I'm clueless which makes me boxless as well. ::grin:: TIA ron
5
by: DavidB | last post by:
Greetings I don't know if this is possible, but this group should be able to tell me. I have a webpage with a changing message board (I understand the problems with having changing text, but...
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
8
by: Sunny | last post by:
Hi, Can someone tell me, How to redefine array or make array empty or null. Here what I am trying to do. var temp = new Array(); for(i=0; i <=outstring.length-1; i++) { temp =...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.