By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

overloading [] or .

P: n/a
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).

Jan 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
VK

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
}

Jan 21 '06 #2

P: n/a


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/
Jan 21 '06 #3

P: n/a
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.)

Jan 21 '06 #4

P: n/a
VK

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.

Jan 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.