I have an object with several methods within it.
I would like to call a specific method based on a variable.
I could use "if, else if" or a switch statement. But I would like to reduce it either one or just a few lines of code using a variable parameter. I am struggling with var function = window[functionstring], .call() and .apply()
The method to be executed will depend on the button clicked by the user.
Can someone show me a simple example.
Yes I know I can just call mdo.a(); when a is clicked, but in this example other processing is occurring before arriving at updatePage();
I would like to call a specific method based on a variable.
Code:
function mapDataObject()
{
this.a = function (){alert('a');};
this.b = function (){alert('b');};
this.c = function (){alert('c');};
this.d = function (){alert('d');};
this.e = function (){alert('e');};
}
var mdo = new mapDataObject;
The method to be executed will depend on the button clicked by the user.
Code:
function updatePage(button)
{
var fn = window['mdo']+'.'+button.id;
fn();
// mdo.call(null, button.id);
// mdo.button.id();
}
Can someone show me a simple example.
Yes I know I can just call mdo.a(); when a is clicked, but in this example other processing is occurring before arriving at updatePage();
Comment