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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:26:02+00:00 2026-06-13T06:26:02+00:00

Possible Duplicate: Why is using the JavaScript eval function a bad idea? So I

  • 0

Possible Duplicate:
Why is using the JavaScript eval function a bad idea?

So I have read through MANY different methods on calling a function from a string, using window[](); and also eval();. I am wondering if for my situation (That is below) what method is exactly the right way to go ahead with, and if so, explain why. Also explain why eval(); isn’t exactly a good option, a lot of people say security, but why would security be an issue if you can get any browser plugin that enables you to change the script on that page? (example: firebug for firefox)

My current code:

funcOne(target).funcTwo(x, y, z);

How would call this using the recommended window[](); way? And why can’t I use this?:

eval('funcOne(target).funcTwo(x, y, z)');

I don’t want your annoyance of this question being asked many times, because I cannot currently think of a way to call a, as a call it, double function.

Thanks in advance!

  • 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-06-13T06:26:03+00:00Added an answer on June 13, 2026 at 6:26 am

    In Javascript the syntax a.b can be replaced with a["b"]. So in your case you can use

    window["funcOne"](target)["funcTwo"](x, y, z);
    

    where of course it makes sense only if you are using variables instead of "funcOne" and "funcTwo".

    If everything is instead fixed but you simply want to delay execution you can use “thunking” with an anonymous closure with

    x = function(){ return funcOne(target).funcTwo(x, y, z); };
    

    and then you can evaluate with x() to get the desired result later.

    The last example will work correctly even if the variables target and x, y and z are local to the enclosing scope because the thunking closure will “capture” them.

    You should however pay attention to the fact that in Javascript the only way to create a new scope is to use a function (a block surrounded with { and } is NOT a scope like happens in C++ and other languages).

    If you need to create several closures in a loop this can bite back and is a source of a quite common mistake…

    for (var i=0; i<options.length; i++)
    {
        var menu_item = document.createElement("div");
        menu_item.textContent = "Option " + i;
        menu_item.onclick = function () {
            // Warning this will NOT work. All divs will
            // alert using the same number!
            alert("Option " + i + " selected");
        }
        menu.appendChild(menu_item);
    }
    

    here I used a closure for the onclick event on the div, but this is not going to work because all those functions will use the very same i variable. Because in Javascript the only way to create a scope is using a function the solution is:

    for (var i=0; i<options.length; i++)
    {
        var menu_item = document.createElement("div");
        menu_item.textContent = "Option " + i;
        (function(i){
             menu_item.onclick = function () {
                 alert("Option " + i + " selected");
             };
         })(i); // Pass current `i` as parameter
        menu.appendChild(menu_item);
    }
    

    This way the variable i inside the onclick handler will be different for each closure.

    This pattern of creating a function just to call it immediately is often used in Javascript when you need to create many independent closures so it’s better to know and understand it.

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

Sidebar

Related Questions

Possible Duplicate: Redirect parent window from an iframe action using JavaScript I have a
Possible Duplicate: How to reload a page using Javascript? I have the Facebook login
Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
Possible Duplicate: When is JavaScript’s eval() not evil? I know, generally using eval() is
Possible Duplicate: Download File Using jQuery Download File Using Javascript/jQuery $('#download').click(function() { var url
Possible Duplicate: How to get the selected value of dropdownlist using JavaScript? I have
Possible Duplicate: How to impose maxlength on textArea in HTML using JavaScript I have
Possible Duplicate: Calling base method using JavaScript prototype I want to inheritance object that
Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }
Possible Duplicate: How can I create a Zerofilled value using JavaScript? I have to

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.