By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Triggering the Execution of Code with The Setting of Object Properties

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.