overloading [] or .

Discussion in 'Javascript' started by Jason S, Jan 21, 2006.

  1. Jason S

    Jason S Guest

    I'm pretty sure the answer is no, but is there any way to overload []
    or . in an object?

    e.g. if I had some object o with a property v, to map the syntax
    o.v["something"] or o.v."something", so that it calls
    o.get('something') or o.set('something',newval)?

    like

    o.v.squash = 3 would actually call o.set('squash',3)
    o.v.squoosh+1 would actually call o.get('squoosh')+1
    o.v[x] += 7 would actually do o.set(x,o.get(x)+7)

    I know about getters and setters, so you can do this for specific
    individual properties of an object (the first two examples above), but
    I'm wondering if there's a way to do it in general (e.g. the third
    example).
    Jason S, Jan 21, 2006
    #1
    1. Advertising

  2. Jason S

    VK Guest

    Jason S wrote:
    > I'm pretty sure the answer is no, but is there any way to overload []
    > or . in an object?


    Not exactly in the way you posted, but you can force a method to act as
    a field, or more precisely to make a field to look like a method... or
    I've got lost... any way here is a sample. Completely useless as it is
    but it had some misterious sense for my twisted brains in one of my
    projects :)

    function over() {
    var $ = this;
    this.$ = 10;
    this.v = function(){};
    this.v.toString = function(){return $.$*10;};
    }

    function test() {
    var o = new over();
    alert(o.v + 1); // displays 101
    }
    VK, Jan 21, 2006
    #2
    1. Advertising

  3. Jason S wrote:


    > I know about getters and setters, so you can do this for specific
    > individual properties of an object (the first two examples above), but
    > I'm wondering if there's a way to do it in general (e.g. the third
    > example).


    Only Spidermonkey implements getters and setters, I think there was some
    talk on jseng that someone is working on a general setter/getter
    extension, bug is here
    <https://bugzilla.mozilla.org/show_bug.cgi?id=312116>
    but so far obviously not implemented but under discussion.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 21, 2006
    #3
  4. Jason S

    Jason S Guest

    This is in the context of jsdb (plug for Shanti's hard work: see
    www.jsdb.org) which uses Spidermonkey, so I do have access to
    __defineGetter__ and __defineSetter__.

    more context info:
    I'm writing (or attempting to write :) some COM objects which jsdb can
    access via its ActiveX object interface. One of those is an associative
    array which has side effects (the actual values in the array are stored
    in a piece of external hardware); I would like to access it easily via
    jsdb, but I don't think I can write a dynamic dispatcher that jsdb
    would understand, & I think I'm limited (as far as the
    jsdb->Spidermonkey->COM interface) to regular methods/properties e.g.
    o.set('var1',o.get('var1')+1).

    I'd like to just be able to say o.v.var1 += 1 or o.v["var1"] += 1, and
    jsdb/Javascript would somehow not try itself to evaluate the .var1
    property but when it saw o.v would call the appropriate dispatch method
    of the object in question & the COM object would handle the lower-level
    access.

    Granted, this adds no new functionality, just "syntactic sugar", but
    that has value in the environment my scripts are going to be used...

    method (1) would be to add the "syntactic sugar" in an encapsulating
    javascript object which does what I want. I guess that's reaching for
    now.
    method (2) would be to handle it on the COM level, based upon the way
    jsdb handles its COM object interface... we'll see how that goes.


    (btw, "$" is a valid identifier in Javascript??!?!?! I guess so, it
    works in jsdb. Huh.)
    Jason S, Jan 21, 2006
    #4
  5. Jason S

    VK Guest

    Jason S wrote:
    > I'd like to just be able to say o.v.var1 += 1 or o.v["var1"] += 1, and
    > jsdb/Javascript would somehow not try itself to evaluate the .var1
    > property but when it saw o.v would call the appropriate dispatch method
    > of the object in question & the COM object would handle the lower-level
    > access.


    JavaScript doesn't provide operator overload technics, so everything in
    this domain will be a fairly non-traditional use of traditional tools,
    I guess it's mutually understood ;-)

    If "pseudo-keys" in <o.v> hashtable are limited by some set, then my
    trick will fly after some further optimization. If "pseudo-keys" can be
    of any kind at runtime, then namely you want [] to work as () and I
    don't see any way to achieve it - unless some dirty use of eval(?)

    > (btw, "$" is a valid identifier in Javascript??!?!?! I guess so, it
    > works in jsdb. Huh.)


    Yes, it is perfectly valid. There is some usage legacy coming from
    proto-Unix through MS-DOS and up to Java that names *starting* with
    $-sign denote machine-generated auxiliary files, so humans :) should
    not use such names to avoid confusion.
    JavaScript / JScript though doesn't have any *internal* tradition or
    implication for $names, so it's up to end developer. I personally use
    $names for cla... constructor members where at one beautiful sunny day
    I will be finally able to put "private" modifier.
    VK, Jan 21, 2006
    #5
    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. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,397
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    423
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    571
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    392
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page