0

I just wanted to ask if it is possible to take a string, which will always be constant and add a function's name to it. an example:

function modifyFunctionName(theFunc)
{
    var newFunc = "link" +theFunc;
    return newFunc;
}
//Somewhere in the HTML:
<input type='submit' value='button' onclick='modifyFunctionName(CheckLogin())'/>

So the modifyFunctionName will return linkCheckLogin().

EDIT: Sorry guys actually what I was exactly trying to do was this: https://gist.github.com/2371187

2
  • 2
    Looks like a good candidate for an XY Problem (mywiki.wooledge.org/XyProblem). What are you really trying to do? Commented Apr 13, 2012 at 15:50
  • It seems, that OP want's to assign a different event handler function to the button, depended on CheckLogin() return value. Commented Apr 13, 2012 at 16:06

3 Answers 3

1

It technically is possibly, but you would have to use eval and that isn't a good idea. What you could do is create an object and just store references to functions and it would work about the same way.

var obj={};

function addFunction(name,func){
  obj[name]=func;
}

function changeName(oName,nName){
  obj[nName]=obj[oName];
  delete obj[oName];
}

addFunction('test',function(){return 'hi'});
obj.test();//returns 'hi'
changeName('test','greet');
obj.greet();//returns 'hi'
Sign up to request clarification or add additional context in comments.

Comments

0
<script>

    function one() {
        alert("one");
    }

    eval("function two() { one();}");
    two();

</script>

Comments

0

based on the question : it is not very clear what you intend to do , anyway , you can eval anything that is attached to a scope :

myFunction(aString){
// window is the scope , the context.
window[aString] = myFunction ;
}

the question is why would you want to do that ?

Comments

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.