2

Let's say I'm given a string, and I want to define a function with a name of that string. How can I do this?

UPDATE:

I forgot to mention that I do not want to set an anonymous function to a property because I would also like to dynamically grab the function's name from inside the function ie. arguments.callee.name

3
  • 1
    If you've already been given the name, why do you need to then obtain that name again from arguments.callee.name? Commented Jun 15, 2012 at 22:12
  • @JamesAllardice because I'm dynamically rewriting the removeChild javascript method of Element's prototype Commented Jun 15, 2012 at 22:13
  • 3
    I'm not entirely clear what you're trying to achieve. Some code in your question might help. But as a side note, don't forget that arguments.callee is deprecated, and throws a syntax error in strict mode. Commented Jun 15, 2012 at 22:17

2 Answers 2

3

Like this:

var functionName = "myfunction"
window[functionName] = function() {
    // your function here
} 

Depending on the requirements, maybe something like this would work:

var myFunctionGenerator = function(name) {return function(message) {
    alert("hi, this is a function named '" + name + "'.  The parameter passed is '" + message + "'.");
}}

var myFunction = myFunctionGenerator('some function name');
myFunction('a parameter');
// hi, this is a function named 'some function name'.  The parameter passed is 'a parameter'.
Sign up to request clarification or add additional context in comments.

7 Comments

ah, I forget to mention that I do not want to set an anonymous function to a property because I would also like to dynamically grab the function's name from inside the function ie. arguments.callee.name
@user730569 Just remember that callee and strict mode... not so good.
@DaveNewton is there an alternative?
@user730569 - The alternative is usually to use a named function expression. But without more information about what you're actually trying to achieve, I think it's going to be difficult to help properly.
@DaveNewton dbaseman's second solution works like a charm... and it doesn't use callee, so it's a win-win :)
|
1

If your declaring the function, use eval(). Just type a string representation of the argument your trying to execute for example:

eval("function name(){alert('a');}");

You can then call that method by normal convention, name();.

If you already have the function name and want to call that function with a string representation you can use the eval() method, although its not always optimal for performance. You will have this:

var fnName = "functionName";
var params = "param1";
var fnNameWithParams = "functionName("+params+")";
eval(fnNameWithParams);

A better approach may be:

var fnName = "functionName";
var params = "param1";
var fnToCall = window[fnName];
fnToCall(params);

1 Comment

"its not always optimal for performance"... it's not optimal for almost anything. Unfortunately, I don't know if there's going to be any way the OP can achieve what they are trying to without it (at least not without more information).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.