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

  • Home
  • SEARCH
  • 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 3218370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:31:12+00:00 2026-05-17T15:31:12+00:00

Alright, to start with let’s look at some code: <html> <body> <script type=text/javascript> function

  • 0

Alright, to start with let’s look at some code:

<html>
    <body>
        <script type="text/javascript">
            function logFunction(fn) {
                console.log(fn.prototype);
                console.log(fn);
                console.log(fn(4));
            }
            var num = 5;
            var add5 = function(x) { return x + 5 };
            var addNum = function(x) { return x + num };
            var adder = function(y) { return function(x) { return x + y } };
            logFunction(add5);
            logFunction(addNum);
            logFunction(adder(5));
        </script>
    </body>
</html>

When executed, it returns the following results:

Object
    constructor: function (x) { return x + 5 }
    __proto__: Object
function (x) { return x + 5 }
9

Object
    constructor: function (x) { return x + num }
    __proto__: Object
function (x) { return x + num }
9

Object
    constructor: function (x) { return x + y }
    __proto__: Object
function (x) { return x + y }
9

While it would be very easy in the first case to see that the value that x will be added to is 5, I cannot seem to come up with a way to do the same with the other two cases.

My question is this: Is there a way to determine the value of what x is being added to in the last two examples, armed only with the function reference and the knowledge of what the variable is called (“num”, “y” etc.)?

EDIT:

Alright, I can see that finding these values is indeed impossible. The only way would be if I could have access to the anonymous function’s ‘arguments’ property, but alas, that too is impossible. My work around for this is to require an Object of type Function as a parameter. There are still some problems with this, as can be seen in my new code below, but this should work for my case. Thanks everyone!

<html>
    <body>
        <script type="text/javascript">
            function logFunction(fn) {
                console.log(fn.prototype);
                console.log(fn);
                console.log(fn(4));
                console.log("");
                console.log("");
            }
            var num = 5;
            var addNumFunc = new Function("x", "return x + " + num);
            var whereNumFunc = new Function("x", "return x >= " + num * num);
            var adderFunc = new Function("y", "return function(x) { return x + y }");
            var adderFuncFunc = new Function("y", "return new Function(\"x\", \"return x + \" + y)");
            logFunction(addNumFunc);
            logFunction(whereNumFunc);
            logFunction(adderFunc);
            logFunction(adderFunc(5));
            logFunction(adderFuncFunc);
            logFunction(adderFuncFunc(5));
        </script>
    </body>
</html>

Which returns:

anonymous
function anonymous(x) {
return x + 5
}
9

anonymous
function anonymous(x) {
return x >= 25
}
false

anonymous
function anonymous(y) {
return function(x) { return x + y }
}
function (x) { return x + y }

Object
function (x) { return x + y }
9

anonymous
function anonymous(y) {
return new Function("x", "return x + " + y)
}
function anonymous(x) {
return x + 4
}

anonymous
function anonymous(x) {
return x + 5
}
  • 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-17T15:31:13+00:00Added an answer on May 17, 2026 at 3:31 pm

    In the case of num:

    num is a global property. If you have that knowledge (that it is a global property), then you know that you can access it like so:

    window.num
    

    In the case of y:

    y is a property that is in the scope chain of the function that was created by executing adder(5). That function has access to the value of y, but the code outside of that function does not have access to it.

    var closureFunction = adder(5);
    

    The function closureFunction has the y property in its scope chain, and if you run this function, the value of y will be added to the passed-in argument. However, if you refactor your code you could enable the retrieval of the value of y.

    Your code:

    var adder = function(y) {
        return function(x) { 
            return x + y;
        };
    };
    

    Refactored code:

    var adder = function(y) {
        var fn = function(x) { 
            return x + y;
        };
        fn.getArgument = function() { return y; };
        return fn;
    };
    

    Now you can get y like so:

    closureFunction.getArgument();
    

    This should be thought trough.

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

Sidebar

Related Questions

Alright, let me start with an example. I have a bunch of items and
Alright, I think we all agree that what happens with the following code is
I'm trying to add a remember me feature to my web app to let
Is it OK to have the following code in my constructor to load an
Alright, I am going to state up front that this question may be too
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, I have my urls set up to rewrite news_item.php?id=1 to blog/1/Title-of-post. This works
Alright, currently I have my SWF hitting a php file that will go and
Alright so I have this C++ image capturing class. I was wondering if I
I am using Puppet for automating configuration management across hosts. It can use erb

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.