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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:36:19+00:00 2026-05-23T23:36:19+00:00

I am needing to find the argument passed to a function from the function.

  • 0

I am needing to find the argument passed to a function from the function.

Let us suppose I have a function called foo:

function foo() {
  var a = 3;
  var b = "hello";
  var c = [0,4];
  bar(a - b / c);
  bar(c * a + b);
}

function bar(arg) { alert(arg) }

As it is now, of course, bar will always alert NaN.

Inside of the function bar, I want to obtain the argument in the form it was originally passed. Furthermore, I want to be able to access the values of a, b, and c from the bar function. In other words, I would like something of this nature:

bar(a - b / c);    

function bar() {
 //some magic code here
 alert(originalArg); //will alert "a - b / c"
 alert(vars.a + " " + vars.b + " " + vars.c); //will alert "3 hello 0,4"
} 

You may not think this is possible, but I know you can do weird things with Javascript. For example, you can display the code of the calling function like this:

function bar() {
  alert(bar.caller);
}

I am willing to bet a few dollars that with some sort of finagling you can get a the original form of the argument and the values of the variables in the argument.

I can easily see how you could do it if you were allowed to call bar in a form like this:

bar(function(){return a - b / c}, {a: a, b: b, c: c});

But that is too convoluted and therefore unacceptable. The way bar is called may not be changed.

  • 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-23T23:36:21+00:00Added an answer on May 23, 2026 at 11:36 pm

    Your question is misguided and sick and…I love it! Here is a short solution which I will explain.

    function foo() {
        var a = 3, b = "hello", c = [0, 4];
        bar(a - b / c); 
    }
    
    function bar(n) {
        alert([n, a, b, c, eval(n)].join('   '));
    }
    
    function meld(f, g) {
        f = f.toString(); g = g.toString();
        f = f.replace(/function\s+\w+/, 'function ');
    
        var n = g.match(/function\s+(\w+)/)[1];
        f = f.replace(new RegExp(n + '\\((.*?)\\)', 'g'), n + '("$1")');
    
        var i = f.indexOf('{') + 1;
        f = f.slice(0, i) + g + f.slice(i);
    
        eval('var ret = ' + f);
        return ret;
    }
    
    foo = meld(foo, bar);
    foo();
    

    The key is the meld(f, g) function which returns a new anonymous function that acts just like f while calling and exposing its internals to a copy of g. You see, the original g is in a bad position to see anything about f — at best it can use g.caller and tons of regular expressions.

    Here is the pseudo code for meld. First make f an anonymous function, suitable for evaluating and assigning to a variable later on; for instance function foo() { becomes function() {. Next find g‘s real name and quote any arguments passed to it from new f. Next, insert a copy of g inside new f. It will mask the global name and it will have access to f‘s local variables as if they were its own. Finally, evaluate this anonymous function and return it.

    So how do we use meld? Just replace foo with meld(foo, bar) and then use foo as you normally would.

    OK, now for the limitations. I didn’t want to spend lots of effort refining the regex inside meld to quote g‘s argument(s) against all possibilities. This would just have been a distraction from the concept of the solution as a whole. You’ll need to change it to properly handle more than one argument and escape any which themselves have quotes or parentheses.

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

Sidebar

Related Questions

I find myself needing a string table in a Haskell program I'm developing. In
I find myself needing to create a View completely in Java without knowing what
Often, I find myself needing to make a tree of directories executable for someone,
Let's say I have the following: ['cart', 'horse'] Is there a quick way to
I am certain this has been beaten to death, but I can't find it.
I'm reading this article and there is a paragraph: If you ever find yourself
I am using a commercial PHP web application that stores information in a mysql
I'm working on a Java application that collects various bits of information on network
I need a semi-shallow copy of an object. Under my original design I used

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.