Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:52:21+00:00 2026-05-16T00:52:21+00:00

For simplicity’s sake, I’ve included a script that dynamically calls a function by its

  • 0

For simplicity’s sake, I’ve included a script that dynamically calls a function by its name:

var foo = "hello";
var bar = "world";
var function_name = "say_" + foo + bar;

// Since its name is being dynamically generated, always ensure your function actually exists
if (typeof(window[function_name]) === "function")
{
    window[function_name](" World!");
}
else
{
    throw("Error.  Function " + function_name + " does not exist.");
}

function say_helloworld(the_word)
{
    alert("Hello " + the_word);
}

But the function say_helloworld‘s code is written in a static fashion. I would like something like:

var function_implementation = 'function say_'+foo+bar+
    '(the_world){alert("Hello " + the_world);}';
eval(function_implementation);

but without using eval(). There’s an even uglier approach: doing an AJAX call to get the function.

Can you see a better approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:52:21+00:00Added an answer on May 16, 2026 at 12:52 am

    You can use an inline function expression:

    window['say_'+foo+bar]= function(the_world) {
        alert('Hello '+the_world);
    };
    

    However, there is almost never a good reason to use dynamically-named variables. Store functions in a separate lookup object instead:

    var says= {
        helloworld: function(the_world) {
            alert('Hello '+the_world);
        },
        somethingelse: function(otherthing) {
            alert('Something else with '+otherthing);
        }
    };
    says[somevar]('potatoes');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the sake of simplicity, let's say that we have input strings with this
For the sake of simplicity say I have multiple Xtext DSLs that all describe
EDIT: For the sake of simplicity: I've got a simple UserJS script (FF/Chrome: Greasemonkey,
for sake of simplicity: public class Person { String name; Set<Address> addresses; } public
for the sake of simplicity let's assume that I'm making a simple Pong clone
Sorry for the simplicity of the question. New to Matlab and despite its thorough
Edit For simplicity: I just want to make the most basic possible cursor that
Suppose I have the following (shortened for simplicity): jQuery(#grid).jqGrid({ ... ondblClickRow: function() { //
I like HeidiSQL software very much for its simplicity, however I miss the phpMyAdmin
I'm working with three tables, and for simplicity's sake let's call them table A,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.