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 7523353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:46:39+00:00 2026-05-30T02:46:39+00:00

Ok hopefully this come across correctly. I am building a universal javascript function that

  • 0

Ok hopefully this come across correctly. I am building a universal javascript function that will build a menu and then also build the functions that each menu item would call. To do this, I need to pass a list of the commands to be called for each option.

So for example:

var thecall = 'alert("hi, this works");';

function myfunction(thecall)
{
  //In here I want to excute whatever commands is listed in variable thecall
 .....
}

I’m sure doing it this way is completely stupid, but I don’t know how else to do this.

Basically, I need my function to perform other functions on a variable basis.

Thanks!!

  • 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-30T02:46:41+00:00Added an answer on May 30, 2026 at 2:46 am

    You can execute arbitrary strings of JavaScript using eval(), but that is not the best solution for you here (it’s almost never the best solution).

    Functions in JavaScript are themselves objects which means you can store multiple references to the same function in multiple variables, or pass function references as parameters, etc. So:

    var thecall = function() {
       alert("hi, this works");
    };
    
    function myfunction(someFunc)  {
       someFunc();    // call the function that was passed
    }
    
    myfunction(thecall);   // pass reference to thecall
    

    Note that when passing the reference to the thecall function there are no parentheses, i.e., you say thecall not thecall(): if you said myfunction(thecall()) that would immediately call thecall and pass whatever it returned to myfunction. Without the parentheses it passes a reference to thecall that can then be executed from within myfunction.

    In your case where you are talking about a list of menu items where each item should call a particular function you can do something like this:

    var menuItems = [];
    function addMenuItem(menuText, menuFunction) {
       menuItems.push({ "menuText" : menuText, "menuFunction" : menuFunction });
    }
    
    function test1() {
       // do something
    }
    
    addMenuItem("Test 1", test1);
    addMenuItem("Test 2", function() { alert("Menu 2"); });
    
    // and to actually call the function associated with a menu item:
    menuItems[1].menuFunction();
    

    Notice the second menu item I’m adding has an anonymous function defined right at the point where it is passed as a parameter to addMenuItem().

    (Obviously this is an oversimplified example, but I hope you can see how it would work for your real requirement.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this will not come across as a silly or pedantic question, but I'm
Well, I'm simply writing this post to hopefully help others that might come across
Hopefully this will not spark a religious war... We have a web based app
hopefully this will be an easy answer for some of you CSS veterans out
Hopefully this explanation will make sense, but what is the best way (if it
Ok I've tried searching this site for other similar questions, but those that come
I have been looking for a javascript (hopefully jQuery) timezone chooser/picker/control widget that I
Hopefully this is a simple one, but can anyone provide some simple c# code
Hopefully this still falls within StackOverflow's umbrella! I'm looking to create a quick boot
Hopefully this is a really quick one ;) I have written a lexer /

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.