overloading [] or .

J

Jason S

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).
 
V

VK

Jason said:
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
}
 
M

Martin Honnen

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.
 
J

Jason S

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.)
 
V

VK

Jason said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top