Request: Implement JavaScript bracket notation


#1

It would be nice if i could access properties of an object in JavaScript using the bracket notation.

var property = object['nameofproperty'];

I guess it's just a matter of forwarding it to the NamedValueSet of the DynamicObject.
While we are at, why can't we directly access the properties of an DynamicObject using that same notation.

DynamicObject* obj = new DynamicObject();
//so instead of this
obj->setProperty("enabled", true);
var isEnabled = obj->getProperty("enabled");

//Could this be done?
obj["enabled"] = true;
var isEnabled = obj["enabled"];

I mean a DynamicObject is in fact a wrapper around the NamedValueSet which does support the reading of a NamedValue using the bracket notation, i see setting of NamedValue is not allowed through it, and i can imagine this is done with a reason.  Although i can't see a strong reason why this isn't allowed.

 

 


#2

obj is a pointer, so it can't have an overloaded operator[]. The best you could do would be (*obj)["xyz"]

But it's also a bit cumbersome to implement this, as it'd involve an intermediate temporary object, which would need to know when it's being used to read vs write the property, and which the compiler may or may not optimise-out correctly. It's all do-able but TBH in this case I prefer the clarity of set/getProperty


#3

Yes ok my bad, but of course it doesn't have to be a pointer. 
I don't mind the get set to much but because the NameValueSet was using it my guess was that it would work with the DynamicObject as well.

Anyway, the original question was of course if we could have the bracket notation in the Javascript Engine, i mean that should be easy to add right. And it's actually an important feature of Javascript.
So please can we? ;-)
 


#4

The original question only talks about DynamicObject, it doesn't mention any javascript syntax (?)

And in javascript, it already supports the map syntax, doesn't it?


#5

C'mon Jules.

Topic title:
Request: Implement JavaScript bracket notation

First Lines in my original post: 

It would be nice if i could access properties of an object in JavaScript using the bracket notation.
var property = object['nameofproperty'];

I'm sorry that i confused you with the additional DynamicObject request, but my original request still stands. Unless you added this to version 4 of Juce the bracket notation does not yet work.
 

 


#6

Ah! I'm not being completely dumb:

var property = object['nameofproperty'];

..actually looks like the type valid C++ that you'd write when dealing with the JS engine, so that's what I thought you meant!

Surprised that it doesn't work, but will have a look when I get chance.


#7

Thanx Jules.