I have a bunch of objects assigned to variables:
var ElectricCar = {
start: function(){ alert('started electric car') }
};
var BioDieselCar = {
start: function(){ alert('started bio diesel car') }
};
I want to call the start method on one of these objects, but I only have a string to work with:
var Starter = {
init: function(carType){
//... start the car that matches car type
}
};
Starter.init('ElectricCar');
How can I call the start method on the appropriate object?
The preferred technique would require refactoring your code slightly.
This technique is preferable because it avoids the use of
eval. By converting your vars to properties of an object, you can reference them by string.