Fafanellu
Fafanellu

Reputation: 433

JS instance name as a parameter

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
    });
}

Upvotes: 2

Views: 156

Answers (2)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34234

Simple answer

function createVariable(name, value)
{
    window[name] = value;
}

// Usage:
createVariable("x", 5);
console.log(x === 5); // true

Explanation

There is a term context in JavaScript. When you access a variable variableName you are actually accessing a variableName property in the current context.

Default context in browsers is window object. So, actually, these statements in a default (outer) context are identical:

var variableName = "Hello World";

document.body.innerHTML += variableName + "<br/>";
document.body.innerHTML += this.variableName + "<br/>";
document.body.innerHTML += window.variableName + "<br/>";

In JavaScript, talking about object property accessing, you may use both dot notation or brackets notation:

var variableName = {
  propertyName: "Hello World"
};

document.body.innerHTML += variableName.propertyName + "<br/>";
document.body.innerHTML += variableName['propertyName'] + "<br/>";

However, the last one allows use of characters that can't be used with dot notation, like spaces. Also, it allows dynamic names.

Combining all above, you can easily instantiate a variable with a dynamic name by assigning a property of default window context:

function createVariable(name, value)
{
    window[name] = value;
}

createVariable('stackoverflow', 'Hello World');
document.body.innerHTML = stackoverflow;

For example, in case of marker, it can be done this way:

function makeMarker (name, latlng, label, title) {
    // not that tricky part anymore
    window[name] = new google.maps.Marker({
        position: latlng,
        map: map,
        label: label,
        draggable: true,
        title: title
    });
}

Upvotes: 1

Fafanellu
Fafanellu

Reputation: 433

Couldn't pass the name to the constructor. However, I was able to use only one function to call the constructor. For those who are interested, the solution is quite simple, even if it is not "beautiful". I just racked my brains too much, instead of taking the shortest path. Here is how I did :

markerName1 = makeMarker(latlng1, "label1", "Title1");
markerName2 = makeMarker(latlng2, "label2", "Title2");

function makeMarker (latlng, label, title) {

//the scope is local
var temp= new google.maps.Marker({
            position: latlng,
            map: map,
            label: label,
            draggable: true,
            title: title
        });
temp.dosomething();
temp.addListener('dblclick', function(event) {
    alert('Damn, you double-clicked me !');
})

//temp is returned 
return temp;
}

Upvotes: 0

Related Questions