471,305 Members | 1,131 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

equivalent to python's __getattr__, __setattr__

aum
Hi,

Does Javascript have any equivalent to Python's __getattr__ and
__setattr__ methods?

In other words, the option to define a method of a class that gets invoked
whenever someone tries to fetch an unknown attribute, or set any attribute
of an instance of that class?

I've looked at __defineGetter__, __defineSetter__, __lookupGetter__ and
__lookupSetter__. It did seem like __lookupGetter__ and __lookupSetter__
might have been helpful, but they only seem to return methods that have
already been set with __defineGetter__ and __defineSetter__.

I need a way for an object to invoke a method when any attempt is being
made to fetch an attribute of that object (or, similar to Python, to fetch
an unknown attribute).

Cheers
aum
Oct 9 '06 #1
1 3306

aum wrote:
Hi,

Does Javascript have any equivalent to Python's __getattr__ and
__setattr__ methods?

In other words, the option to define a method of a class that gets invoked
whenever someone tries to fetch an unknown attribute, or set any attribute
of an instance of that class?
No. There is a relevant thread here:

<URL:
http://groups.google.com/group/comp....14aac6a0a90557
>
The most relevant response was from petermichaux:

<quote>
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. :
</quote>
--
Rob

Oct 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Greg Brunet | last post: by
6 posts views Thread by Dave Benjamin | last post: by
5 posts views Thread by Tobias Windeln | last post: by
32 posts views Thread by Kalle Anke | last post: by
267 posts views Thread by Xah Lee | last post: by
122 posts views Thread by Edward Diener No Spam | last post: by
2 posts views Thread by Stef Mientki | last post: by
reply views Thread by rosydwin | last post: by

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.