equivalent to python's __getattr__, __setattr__

Discussion in 'Javascript' started by aum, Oct 9, 2006.

  1. aum

    aum Guest

    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
     
    aum, Oct 9, 2006
    #1
    1. Advertising

  2. aum

    RobG Guest

    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...bject Properties&rnum=1&#doc_4314aac6a0a90557
    >


    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
     
    RobG, Oct 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand
    Replies:
    0
    Views:
    438
    Anand
    Dec 18, 2003
  2. Chris Young

    Recursion with __setattr__

    Chris Young, Oct 16, 2005, in forum: Python
    Replies:
    0
    Views:
    379
    Chris Young
    Oct 16, 2005
  3. Thomas Heller

    __getattr__, __setattr__

    Thomas Heller, Oct 20, 2005, in forum: Python
    Replies:
    3
    Views:
    706
    Thomas Heller
    Oct 20, 2005
  4. Maksim Kasimov

    __getattr__ equivalent for a module

    Maksim Kasimov, Jan 15, 2007, in forum: Python
    Replies:
    2
    Views:
    248
    Maksim Kasimov
    Jan 15, 2007
  5. Bruno Desthuilliers

    __getattr__, __setattr__ and pickle

    Bruno Desthuilliers, Aug 12, 2008, in forum: Python
    Replies:
    6
    Views:
    393
    Bruno Desthuilliers
    Aug 13, 2008
Loading...

Share This Page