javascript is it possible to use a string to call a object function

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?

Answers


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:

var string = 'notSaved';
var funcs = {};

funcs.save = new function(){
    this.alert = function(){
        alert('called save.alert()');
    };
    return this;
};

funcs.notSaved = new function(){
    this.alert = function(){
        alert('called notSaved.alert()');
    };
    return this;
};

funcs[string].alert();

See working example on jsfiddle.

If your variables are global (they should not), they are also automatically enclosed within window object, 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 using eval()).


Sure you can. Try something like this:

window[string].alert();

Looking at your code it's hard to tell what you're actually trying to achieve. Nonetheless, here are a few ideas that may be relevant.

First, let's make a couple of objects:

var rabbit = {
  name: 'Peter',
  hop: function () {
    return this.name + ' hopped!'
  },
  jump: function () {
    return this.name + ' jumped!'
  }
}

var hairy_maclary = {
  name: 'Hairy Maclary',
  jump: function () {
    return this.name + ' jumped over the fence!'
  }
}

Now, you could define a function which invokes the hop method on whichever object is passed to it:

function hop(object) {
  return object.hop()
}

hop(rabbit) // 'Peter hopped!'

I'm not sure why you'd do this rather than invoking hop directly, but perhaps you want to do extra stuff before or afterwards.

If you wanted to you could create a completely generic function which would invoke a given method on a given object:

function invokeMethod(object, method) {
  object[method]()
}

invokeMethod(hairy_maclary, 'jump') // 'Hairy Maclary jumped over the fence!'

This is a really strange thing to want to do, though. Perhaps you could provide more of an idea of what you're actually trying to do, since your example code is rather odd.


Need Your Help

How te keep old app version after releasing new update on iPhone

iphone distribution

Is it possible that users who bought an app can keep the old version and also the new one?

Using Caliburn.Micro binding feature on an “inner” user control

c# .net user-controls caliburn.micro

I'm quite new to Caliburn.Micro, so I guess this has a simple answer (or at least I hope it has :))