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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:58:58+00:00 2026-06-03T12:58:58+00:00

I understand the following codes: function subtract(p1, p2) { return p1 – p2; }

  • 0

I understand the following codes:

function subtract(p1, p2) { return p1 - p2; }
function calculate(p1, p2, calculator) { return calculator(p1, p2); }
var result = calculate(2, 3, subtract); //-1

But I don’t understand the following codes about how p1 and p2 can be recognized as 2 and 3:

var result = calculate(2, 3, function (p1, p2) { return p1 - p2; }); //-1
  • 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-03T12:59:01+00:00Added an answer on June 3, 2026 at 12:59 pm

    UPDATE to address comment:

    May be my question was not clear enough. Let me rephrase it at the other way. If the Calculate has 5 arguments: function calculate(p1, p2, p3, p4, calculator) { return calculator(p1, p2); } Will it be possible to be called like: var result = calculate(2, 3, 9, 5, function (p4, p3) { return p3 – p4; }); //4 not -1 My actual question is: how can the one of parameters uses other parameters as its arguments and distinguish which parameter should become which argument?

    The reason that the anonymous function knows to use the p1 and p2 arguments is because the parameters from the calculate function definition are what is passed into the anonymous function as arguments in the body of calculate.

    Perhaps a clearer way to understand what is happening here is to change the name of the parameters in the function definition of the anonymous function so that those same parameter names aren’t used in the function definition of your anonymous function. This creates a lot of confusion and is often done purposely in Computer Science textbooks when teaching about principles of programming languages and abstractions.

    In addition to changing the parameter names in the anonymous function, changing the calculate parameter “calculator” to the parameter name “fn” may also help avoid confusion between calculate and calculator. Consider your function:

    Function Definition:

    function calculate(p1, p2, p3, p4, fn) {
    
        // here you can see that calculate's p1 and p2 arguments (above) are bound
         // to whatever function we pass into the fn parameter.
          // Whatever values p1 and p2 represent are passed into the function "fn"
           // and are assigned to the parameters p1 and p2 of fn.
        return fn(p1, p2); 
    }
    

    Function call calculate, with anonymous function:

    How do we know which of the first 4 arguments get passed into the anonymous function vs which arguments aren’t used?

    // so here, we pass in an anonymous function that takes 2 parameters, one 
     //and two. We know from the definition above that whatever values are
      // passed in as calculate's p1 and p2 parameters are passed into fn, 
       //so one and two are assigned the values 2 and 3 respectively,
        // since p1 and p2 were assigned 2 and 3, and p1 and p2 were passed into
         // fn in calculate's function definition above.
    calculate(2, 3, 9, 5, function(one, two) {
        return two - one;
    });
    
    
    // We know the values assigned to p1 and p2 are passed into the fn function
     // as the parameters of the anonymous function known as one and two.      
    var result = calculate(2, 3, 9, 5, function(2, 3) {
        return 3 - 2;
    }
    

    See return fn(p1, p2); in the function definition for calculate, at the top of this answer, as a reminder of why the values assigned to p1 and p2 are passed into the anonymous function.

    Thus, the anonymous function returns 3 – 2 = 1.

    For further reading on how this concept can create abstractions, see Joel Spolsky’s article – Can Your Programming Language Do This? Joel does a great job of explaining why JavaScript is so awesome!

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

Sidebar

Related Questions

The following code produces the result I don't understand this. Any ideas why? If
I don't understand why the following code generates a warning. interface Generic<T> { }
The following code crashes and burns and I don't understand why: DateTime dt =
I have the following code and I can't understand what does it mean: var1
I am having following code but unable to understand as to why no match
I don't understand why in the following code, when I instanciate an object of
I'm unable to understand the following codes $(this).animate({rotate3Di: degrees}, options) . Here rotate3Di is
I do not understand this code snippet : function ms(){ var plc=unescape('. unescape( '\x43\x43\x43\x43\n.............\xEF'.
I understand the following Python code: >>> class A(object): ... def __str__(self): ... return
Please help me to understand why the following code works: <script> var re =

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.