473,386 Members | 1,745 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,386 software developers and data experts.

Triggering the Execution of Code with The Setting of Object Properties


Subject line would seem to say it all:

How does one trigger the execution of a method within an object or any
other code/function with the setting of an object property?
More elaboration for those who want it:

Suppose we have anObjectType, with a property .description, whose value
can be a string, and the value of .description actually describes the
structure of anObjectType as a string. (The details are not important
about how the structure is described, but if you want something concrete,
think of 'anObjectType' as a DOM Node of type NODE_ELEMENT, and
..description as the property .innerHTML.)

When an instance of anObjectType is created, the value of description is
created with the instance. Indeed, the value of description may not be
set with the construction of anObjectType, but it might be added to the
prototype of anObjectType.

But the property .description is not merely read-only. Fetching the value
of .description returns the string which describes the structure of the
object in a meaningful way.

When .description is set with a value of type string, the string is
checked to see if it properly describes a legally formed structured for
anObjectType, and the instance (and only that instance) of anObjectType is
completely re-built: it might possibly be destructed and then re-
constructed, or all properties (but not methods) which involve descendant
objects are changed to conform to the described structure.

The question is, the setting of a string value of the property
..description of object anObjectType does not automatically trigger a
method/methods (or an exception??) for doing something (such as
restructing the object) as a result of the setting of an object property.

Or can it?
Sep 22 '06 #1
3 1438
Hi,

Patient Guy wrote:
Subject line would seem to say it all:

How does one trigger the execution of a method within an object or any
other code/function with the setting of an object property?
More elaboration for those who want it:

Suppose we have anObjectType, with a property .description, whose value
can be a string, and the value of .description actually describes the
structure of anObjectType as a string. (The details are not important
about how the structure is described, but if you want something concrete,
think of 'anObjectType' as a DOM Node of type NODE_ELEMENT, and
.description as the property .innerHTML.)
I think the details are important. If it is a browser-provided object
compared with a "class" that you write yourself.

I don't think there is a general purpose observer that can watch any
object for a change in a particular property value.
When an instance of anObjectType is created, the value of description is
created with the instance. Indeed, the value of description may not be
set with the construction of anObjectType, but it might be added to the
prototype of anObjectType.

But the property .description is not merely read-only. Fetching the value
of .description returns the string which describes the structure of the
object in a meaningful way.

When .description is set with a value of type string, the string is
checked to see if it properly describes a legally formed structured for
anObjectType, and the instance (and only that instance) of anObjectType is
completely re-built: it might possibly be destructed and then re-
constructed, or all properties (but not methods) which involve descendant
objects are changed to conform to the described structure.

The question is, the setting of a string value of the property
.description of object anObjectType does not automatically trigger a
method/methods (or an exception??) for doing something (such as
restructing the object) as a result of the setting of an object property.

Or can it?
I think you want to use a setter method to encapsulate the change and
whatever else needs to happen. So for a class you write yourself.

Foo.prototype.setDescription = function(val) {
if (this.validate(val)) {
this.description = val;
// now do other stuff here.
}
};

Perhaps you have to wrap the browser provided objects in your own class
so that you can then have a setter method.

Peter

Sep 23 '06 #2
On 2006-09-22 16:09:32 +0200, Patient Guy <sevisen.adam@gmailDOTHEREcomsaid:
>
Subject line would seem to say it all:

How does one trigger the execution of a method within an object or any
other code/function with the setting of an object property?
The watch method seems to be just what you want. It is a native method
of the Object object, therefore "inherited" by every JavaScript object.
I haven't tested it, but you should be able to do this :

anObjectType.description="some initial value"

anObjectType.reconstruct = function (prop,oldval,newval)
{
... code that rebuilds the object ...

if(newval is a valid description)
return newval
return oldval
}

anObjectType.watch("description", anObjectType.reconstruct)

The function passed to the watch() method will be executed whenever the
object's property named "description" is modified. That function will
be passed 3 arguments : the name of the property, its current value,
and the value that is about to be assigned to it.
Instead, the return value of the function will be assigned to the propoerty.
here anyway.

--
David Junger

Sep 24 '06 #3
Touffy wrote:
Patient Guysaid:
>>
Subject line would seem to say it all:

How does one trigger the execution of a method within an
object or any other code/function with the setting of an
object property?

The watch method seems to be just what you want.
<snip>

Which makes it a pity that it is a non-standardised feature of
JavaScript(tm) that has never been implement in JScript and could not be
expected in any ECMAScript implementation.

Richard.
Sep 24 '06 #4

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

Similar topics

5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
1
by: John Marble | last post by:
I looked around for a way to change the format and the decimal setting of a REAL type variable, but I can't seem to be able to find the synthax anywhere. Here is an exemple of what I am doing: ...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
1
by: Danko Greiner | last post by:
Thanx Willy, this was very helpful. But i also need (and want to know) how to do this from code. Can you plase give me right topic in MSDN? is there good example? Thanx p.s. this is...
3
by: John | last post by:
Hi On my webform, in design mode when I double click on the button component, it creates a click sub for me. But when I add code to the click event and run the web form the code does not seem to...
8
by: Steve Schroeder | last post by:
For some reason I cannot get the OnSelectedIndexChanged event to fire for a listbox I have on a page. I'm able to populate the listbox with data from a stored procedure, but cannot trigger the...
11
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in...
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.