473,779 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.onNameChang e = function() {...}
obj.name = "Sam"; //<= I want to triger obj.onNameChang e automatically.

If this is via a function:

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

Thanks.

Sam

Oct 23 '06 #1
1 2058
Sam Kong wrote:
var obj = new MyClass;
obj.onNameChang e = function() {...}
obj.name = "Sam"; //<= I want to triger obj.onNameChang e 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.prototy pe.__defineSett er__ != 'undefined') {
MyClass.prototy pe.__defineSett er__(
'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
7047
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
7933
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 header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
10
3606
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 language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
6
5191
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
3069
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 the initialization of the control does not dirty the form? I don't want to prompt the users for saving changes unless he changed one of the remaing controls on the form. BTW, the control I initialize is a foreign key and changes over time. Thank...
13
3519
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 following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
6
4967
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 event of a datatable, from two seperate classes. I wish to do some data validating. Due to the nature of the data validation being done, I seperated this code in two classes.
4
1844
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 soon, I'm going to fail this class and the class is already dragging down my GPA. I'm taking an introductory visual basic class with an advanced visual basic book and assignments (the department dropped the prerequisite and the class needs one....
5
991
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 Object, ByVal e As System.EventArgs) Handles Something.Happened 'code here changes some properties of myClass RaiseEvent SomethingsGoingOn()
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.