473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3790

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.s etDescription = function(val) {
if (this.validate( val)) {
this.descriptio n = 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3360
by: Greg Brunet | last post by:
In adding the ability to refer to field values using dbfFile.field notation, I learned how to use __getattr__ and __setattr__ . After some trial and error, I got it working. But as part of my trials, I added some print statements to debug stuff. The ones I added to __setattr__ work as expected, but the one in __getattr__ seems to get called just under 1000 times for every __getattr__ call! Something is obviously not right here - but...
6
2108
by: Dave Benjamin | last post by:
Hey good people, I've been doing a lot of simultaneous Jython and CPython programming lately, and just wanted to say, with no intended ill will toward any of the individuals who have been generous enough to make the two languages possible, that, well, they're kinda different. I guess it was inevitable, but with Jython stuck at Python 2.1, it's not really the same language as CPython is today. You still have to type "from __future__...
5
1692
by: Tobias Windeln | last post by:
Hi! I'm looking for suggestions on object-based inheritance in Python. Automatic forwarding (often called delegation) in Python is easy: def __getattr__(self, attr): return getattr(self.delegatee, attr) def __setattr__(self, attr, value): return setattr(self.delegatee, attr, value)
7
1574
by: Carlos Ribeiro | last post by:
I'm doing some experiments with mini declarative languages (as explained by David Mertz in http://www-106.ibm.com/developerworks/library/l-cpdec.html) in Python, with the intention to use it as the mechanism to define data entry forms. My final goal is to have a simple approach to automatic generation of visual interfaces. The complete framework is rather big, so let's us focus at this specific problem. -- I would like to describe my...
0
2720
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
32
3337
by: Kalle Anke | last post by:
I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something similar to this int age( ) { return theAge }
267
10778
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
122
7401
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection outside of that class. This structured way allows visual tools to host components, and allows programmers to build applications and libraries visually in a RAD environment. The Java language has JavaBeans as its component model which allows Java...
2
2668
by: Stef Mientki | last post by:
hello, I tried to find an easy way to add properties (attributes) to a number of different components. So I wrote a class, from which all these components are derived. By trial and error I created the code below, which now works, but there is one thing I don't understand: in the line indicated with "<<== 1" I'm not allowed to use for item in self.extra_getters :
0
8917
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
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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
6022
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
4525
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.