3

I am trying to create some functions based on variable names, something along the lines of:

var functionsToRegister = ["foo","bar"];

var index;
for (index = 0; index < functionsToRegister.length; ++index) {
    var function_name = functionsToRegister[index];

    PART_IM_MISSING = new Function('return alert("hello, world!");');
}

The client code calls specific methods so for example it will call foo()

Please note that I am not working on a website but on a native windows application (SoftOne) that allows some customization options through a javascript engine.

Also there is absolutely no way to change the client code calling the functions.

5
  • 1
    Would using an object/array not work? functions[function_name] Commented Sep 19, 2016 at 9:46
  • Generate an object with the corresponding keys? Commented Sep 19, 2016 at 9:46
  • @evolutionxbox @LPK would that allow client code to just run foo() or would it need to be called as functions["foo"]? Only the former would work. Commented Sep 19, 2016 at 9:49
  • The client code would need to run functions.foo() Commented Sep 19, 2016 at 10:02
  • @evolutionxbox this is absolutely impossible in this scenario Commented Sep 19, 2016 at 10:05

3 Answers 3

8

You could use the window object, to make a global function.

var functionsToRegister = ["foo", "bar"],
    index,
    function_name;

for (index = 0; index < functionsToRegister.length; ++index) {
    function_name = functionsToRegister[index];
    window[function_name] = new Function('return alert("hello, world!");');
}

foo();

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

4 Comments

Thank you for your fast reponse. Sadly, after testing it I found that the window object is not defined. Is there any way to work around that?
please add the platform.
@anpel - The "window object is not defined"? What platform are you running? Not a browser me thinks.
@evolutionxbox indeed. As I mentioned it the question it is a windows application.
2
var functionsToRegister = ["foo","bar"];

var index;
for (index = 0; index < functionsToRegister.length; ++index) {
    var function_name = functionsToRegister[index];

    this[function_name]=function() {
        return alert("hello, world!");
    }
}

This will add functions foo and bar to the this object (or you can use any object like window),

but I guess you don't want n identical functions.. so maybe:

var functionsToRegister = {
    'foo':function() {return alert("hello, foo!");},
    "bar":function() {return alert("hello, bar!");},
};


var index;
for (index in functionsToRegister) if(functionsToRegister.hasOwnProperty(index)) {
    var function_name = index;
    var function_code = functionsToRegister[index];


    this[function_name]=function_code;
}


this['foo']();
this.bar();

foo();

8 Comments

@evolutionxbox sure it would! Updated snippet.
@evolutionxbox amd if you are in a browser and use window, also foo() would work
@FrancescoMM thank you for your time. In the context of this problem sadly the client can only call foo() so this will not work here.
@FrancescoMM if I was in a normal browser window I would be a happy man. Sadly, as mentioned in the question "I am not working on a website but on a native windows application that allows customization options through a javascript engine. "
@FrancescoMM this ended up being the way to do it. the window object was indeed not available. Thank you for your time.
|
0

You can use eval.

eval('var '+name+'=function(){alert("hello");};');

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.