0

How can I execute a Javascript function such this:

cursor.continue(parameters)

By using a string to identify the function name, without using eval? Something like this:

cursor.callMethod("continue", parameters);

Is this possible?

2
  • @MelanciaUK: I don't believe it is a duplicate of that question. Commented Jan 10, 2014 at 13:29
  • @Ian I've retracted my vote. Cheers. Commented Jan 10, 2014 at 13:51

3 Answers 3

7

Yes, you can use the square bracket notation.

cursor["continue"](parameters)

cursor["continue"] is exactly the same as cursor.continue.

Sign up to request clarification or add additional context in comments.

Comments

2

If you are in control of callMethod, and the function belongs to an object or is global, then yes, that's possible.

For example, if the target function is a method of the same object where callMethod is:

var cursor = {
    callMethod: function(method, params) {
        this[method].apply(this, params);
    },
    continue: function() {}
}
cursor.callMethod("continue", [1, 2, 3]);

1 Comment

Unfortunately the cursor is an IndexedDB cursor, so techfoobar's solution is more appropriate, but definitely +1 for the literal implementation of my example method.
0

Yes, you can call the function like so:

cursor["continue"](parameters);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.