Is there a way to call a function which will call a class constructor, giving it the arguments, especially the instance name, provided in the call ?
The most important thing is that I want to pass the name of the instance I want to be created.
In other words, is there a way to have this working :
function instantiationOfSomething(instanceName, param1, param2, param3) {
instanceName = new mySomethingClass({
...
});
}
//call #1
instantiationOfSomething("circle", 2, 8, 87);
//call #2
instantiationOfSomething("ellipse", 2, 85, 87);
I don't have any possibility to act on the constructor, e.g. instantiationOfSomething(...) is not a part of my code but is a part of an API.
Here is a real code example (Google Maps API v3):
makeMarker("markerName1", latlng1, "label1", "Title1");
makeMarker("markerName2", latlng2, "label2", "Title2");
function makeMarker (name, latlng, label, title) {
//here is the tricky part
instanceName = new google.maps.Marker({
position: latlng,
map: map,
label: label,
draggable: true,
title: title
});
}
evalto create a variable name dynamically, I'd prefer using objects here.callandapplyfunctions since they both allow you to specify the object as the first argument?window.ellipse = instantiationOfSomething(2, 85, 87);and in the called functionreturn new mySomethingClass({...});