I have a generic function which can speak to multiple other functions in appropriate objects is it possible to use a string to call the appropriate function.
var string = "save";
var generic = (new function (string) {
string."alert()";
return this;
})
var save = (new function (string) {
this.alert = (function () {
alert("your document has been saved")
return this
})
return this
})
var notSaved = (new function (string) {
this.alert = (function () {
alert("your document has not been saved")
return this
})
return this
})
I am using it for a far more complex set up but here is an example. Is this possible?
You can enclose your functions within some object so you can access by passing name of the property using some variable (in this case named
string), eg. like that:See working example on jsfiddle.
If your variables are global (they should not), they are also automatically enclosed within
windowobject, so you can call them also like that:window[string].alert(). This will not work for non-global functions (in this case my solution seems to be the only one not usingeval()).