473,324 Members | 2,248 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,324 software developers and data experts.

How can I trigger an event handler when a property changes?

Hi,

I want to trigger an event handler when a property changes.
If the property is set via a function, it's simple to trigger the
attached event handler.
But if the property is set directly, I don't know how.
What I want is like the property in C#.

var obj = new MyClass;
obj.onNameChange = function() {...}
obj.name = "Sam"; //<= I want to triger obj.onNameChange automatically.

If this is via a function:

var obj = new MyClass;
obj.onNameChange = function() {...}
obj.setName("Sam"); //<= I can trigger an event handler in the
function.
I hope I described what I want well.

Thanks.

Sam

Oct 23 '06 #1
1 2020
Sam Kong wrote:
var obj = new MyClass;
obj.onNameChange = function() {...}
obj.name = "Sam"; //<= I want to triger obj.onNameChange automatically.
Spidermonkey, the JavaScript engine used in Mozilla supports getter and
setter functions e.g. a setter for the |name| property with

function MyClass () {}
if (typeof MyClass.prototype.__defineSetter__ != 'undefined') {
MyClass.prototype.__defineSetter__(
'name',
function (value) {
alert('Setter called with "' + value + '".');
return value;
}
);
}

var obj = new MyClass();
obj.name = 'Kibo';

But you can't use that on the Web as other implementations (e.g. MS
JScript or Opera's engine) do not support __defineSetter__. So unless
you write extensions for Mozilla or script in another environment where
Spidermonkey is used as the JavaScript engine you don't have
getters/setters.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 23 '06 #2

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
6
by: amith | last post by:
hi, i have some 10 radio buttons meant to take the rating from the user. ex: 1 2 3 4 5 6 7 8 9 10 looks O O O O O O O O O O 1 2 3 4 5 6 7 8 9 10 features O O O O O O O O O O
6
by: P | last post by:
Hi, I have a form with several controls. One of them is initialized through VB in frm_current. I am using the frm_beforeupdate event to prompt users for saving changes. How can I make it so that...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
4
by: LyzH | last post by:
Someone else had a question on how to emulate a mouse click. I tried posting in that thread but I have something of a twist on this problem and I'm really in trouble here! If I don't get help...
5
by: govolsbaby | last post by:
Not sure how to ask this so I'll try a couple ways. So I have a class with an event... Public Class myClass Public Event SomethingsGoingOn() Private Sub SomethingHappened(ByVal sender As...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.