JavaScript Engine Feature Request - global constructor methods

Can we please have a way to define global constructor methods, as opposed to everything being related to a DynamicObject? Right now, you have to execute some native code when the engine starts. Seems messy to me, having inline JS code.

The idea would be that a DynamicObject also has a constructor function, so it can be made from JS code as well as C++.

Or is there a way to do this I missed?






I don't understand.. That's not how JS works. The language doesn't really have "constructors", you just have prototype objects which you clone or inherit from.

Right. JS doesn't have objects per se, so people mimic them in various ways, as your engine is doing with DynamicObjects.

But on the juce side there's dynamic objects, which have certain properties we can choose to add as if there it's an object. I don't see a way to instantiate those from the JS side. 

There are some loose conventions in JS, such as global functions that creates a JS object and fill in methods and members as needed.

I'll be more concrete. Let's say in my app I have a class 'bicycle'. A bicycle has members style, gears, horn, and methods pedal and break.

In C++ I can create a dynamic object for each bicycle instance I want and pass them to the engine, and the JS user can access them. I would do that with a DynamicObject subclass that adds the right stuff. But - what if the JS user wants to make a 'new' bicycle? Cloning is a poor (and non-standard) approach as it will inherit anything that has been done to the particular bicycle they chose to clone (and keeping a 'clean' one around is more work for the user). Recreating all the steps that I do in C++ is risky - what happens if a member is added?

So - my DynamicObjects do have a Constructor. I would like to access that Constructor in JS, such as:

var Chopper = Bicycle ("Cool", 4, 11);

var Grifter = Bicycle ("Shite", 3, 2);

It isn't pure JS, no, but it's a common way to mimic objects in JS that happens to align with real world use and Juce's DynamicObjects.



PS - I did indeed receive a Grifter for Christmas one year, when I, and every other boy, actually wanted a Chopper.




Digging with google a tiny bit more, Object Oriented Javascript seems close to standardizing on using Prototypes:

Maybe my request is a way to add a Prototype to the engine? That way the user does:

var Chopper = new Bicycle ("Cool", 4, 11);

Note the added 'new' keyword.

BTW - I see and acknowledge your point about JavaScript 'not having objects', but that doesn't stop most applications from needing objects, especially when embedded in a C++ application.


But it already does support the 'new' keyword (?)

But that only works with functions I've made on the JS side? I think it would be useful to register a DynamicObject subclass so the new keyword will make a new one from JS code. 

Hmm, to do that each registered class wou need some kind of factory class that can take parameters create these objects.. No time to do it right now, but maybe you feel like having a go if it's useful for what you're building?

I think it would be useful for everyone! I'll take a look. I could probably do it with an object called 'Factory' and then the factories methods could be installed as global somehow.

I'll dig.


Please don't go using any globals if you want me to actually merge your changes into juce!

Very usefull request Bruce, that's what i was trying to say in our previous discussion. 

Although i now that Jules said he didn't wanted to compete with the big boys it might be good to see how they do things.

this is a blog post explaining how to do deal with this in v8.

Please don't go using any globals if you want me to actually merge your changes into juce!


Not globals to the app, but a root set of functions for the engine, so I can do Chooper = new Bike() as opposed to Chopper = Factory.Bike(0 or similar.

So - no C++ globals, no.