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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:17:23+00:00 2026-06-14T03:17:23+00:00

I am reading Eloquent Javascript and am having a difficult time understand the example

  • 0

I am reading Eloquent Javascript and am having a difficult time understand the example below. Would anyone be able to do a line by line type explanation? Specifically, I’m confused as to why the first loop is starting at one, and why the push method is being used on both knownArgs and arguments. I know that this is related to “partial application”, but would like a more detailed explanation of what exactly is happening line by line.

var op = {
 "+": function(a,b){return a + b;}
};

function partial(func) {
 var knownArgs = arguments;

 return function() {
  var realArgs = [];

  for (var i=1; i<knownArgs.length; i++)
   realArgs.push(knownArgs[i]);

  for (var i=0; i<arguments.length; i++)
   realArgs.push(arguments[i]);

  return func.apply(null, realArgs);
 };
}

map(partial(op["+"], 1), [0, 2, 4, 6, 8, 10]);
  • 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-14T03:17:24+00:00Added an answer on June 14, 2026 at 3:17 am

    The knownArgs variable keeps a copy of the value of arguments as it was when partial() was called. That call returns another function, and inside that code arguments are a whole different list — they’re the arguments passed to that returned function. In other words:

    var p = partial(someFunction, “hello”, “world”);

    When p() is called, knownArgs will be “hello” and “world” (well and someFunction too but note that the first loop starts at 1). If the call to p() looks like this:

    p(“how”, “are”, “you”);

    then it will first push “hello” and “world” onto the realArgs list (from knownArgs), and then the three parameters passed to p(), from arguments.

    edit — step-by-step breakdown of how map(partial(op["+"], 1), [0, 2, 4, 6, 8, 10]); is evaluated:

    1. First, op["+"] has to be evaluated. I’m guessing it returns a function, probably something like this:

      function add(a, b) {
        return a + b;
      }
      
    2. That “add” function and the value 1 are passed to partial(). Thus inside partial() the arguments pseudo-array looks like

      [ add, 1 ]
      

      That is, the first parameter is the “add” function from op["+"] and the second is that value 1. The only thing that partial() really does before returning the anonymous function is to save arguments into knownArgs. That has to be done because the weird arguments pseudo-variable is always assigned a new value upon each and every function call. It’s being preserved here so that the code in the anonymous function can access it later.

    3. Now, with the anonymous function returned from partial() and that array of even numbers, we call map(). That function probably looks something like this (I don’t have the book):

      function map(fn, list) {
        var i, result = [];
        for (i = 0; i < list.length; ++i) {
          result.push( fn( list[i] ) );
        }
        return result;
      }
      

      Inside map(), then, the first parameter is the anonymous function returned from the earlier call to partial(). What does that function do? Well, it combines the parameters from the original partial() call — except the first one — with the parameters passed into it. The map() function only passes one parameter, so the resulting parameter list on each call to the anonymous function will be the value 1 passed to partial() and then, on each iteration, a different even number from the list.

    A simpler example would be to consider what happens when you call:

    partial(op["+"], 1)(2);
    

    That is, if you call partial() and then immediately use its return value (the anonymous function). The effect will be the same as calling:

    add(1, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading Eloquent JavaScript and I came across this example for the puzzle:
While reading Eloquent Javascript (Chapter 6) I am trying to grasp fundamental concepts. There
I'm reading the Eloquent Ruby book (awesome book so far) and don't understand one
Reading manual about Sling http://sling.apache.org/site/46-line-blog.html added folder blog and blog.html to destination: \launchpad\content\src\main\resources\content\ but
Reading the Scala by Example book and there is this example when Martin explains
Reading Real World Haskell, on page 95 the author provides an example: myFoldl f
Reading Chomsky hierarchy ... ... I know regexp can't parse type-2 grammars (context-free grammars),
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading PHP & jQuery Cookbook right now and it gives this example for using
Reading this brief example of using memcached with PHP, I was wondering how memcached

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.