Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14754

Re: Question about max()

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Question about max()
Date 2012-06-28 20:00 +0200
Organization albasani.net
Message-ID <jsi63d$fqc$1@news.albasani.net> (permalink)
References <njnou7tungmkib8ip6tbbq7oqufah4d027@4ax.com> <jshq3n$m1k$1@news.albasani.net> <di3pu7lo5mjq22e92lsk17m37mhk3qoa90@4ax.com>

Show all headers | View raw


On 2012-06-28 19:19, Mason Barge wrote:
> Okay, I get all that and I understand how to use it.  What is perplexing
> is that, unlike a normal function parameter, the first parameter in the
> callback function is never explicitly assigned a value in the code. 
> 
> In other words, there is no "value =" expression anywhere. If I did this
> as a loop, at some point  there would be something like "value = i" or
> "value = array[i]" in the code.
> 
> So (I assume) map() must silently assign it a value equal to the current
> array item. Am I completely off track?
> 
> Is this a common feature of callback functions?  

It's a common feature of all function calls.
Look at this trivial case:

  function test(val) {
      alert(val);
  }
  test(42);

There's no "val=42" assignment here, either, because the argument value
is assigned based on parameter position.

The map() example is a little harder to understand, because you can't
see the code inside the map() function. But you can build your own map()
function, which should make things clearer:

  // (simplified, see below)
  Array.prototype.myMap = function (callback) {
      // note: "this" is the "even" array below (in this example)
      var result = [];
      for (var i in this) {
          result[i] = callback(this[i], i, this);
      }
      return result;
  };

  var even = [2, 4, 6, 8];

  var odd = even.myMap(function (value) { return value + 1; });

  alert(odd.join(", "));  // 3, 5, 7, 9

Now you can see how the callback function is called with three
parameters, the first being the value of the current array element.
Still no "value=" assignment, because none is needed.

Note that I left out the thisArg parameter and a number of checks to
make the example simpler. If you're interested, you can find a more
complete implementation on the MDN site:
<https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map>


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Question about max() Mason Barge <masonbarge@gmail.com> - 2012-06-28 09:49 -0400
  Re: Question about max() Mike Winter <mlwinter@gmail.com> - 2012-06-28 07:08 -0700
  Re: Question about max() Stefan Weiss <krewecherl@gmail.com> - 2012-06-28 16:35 +0200
    Re: Question about max() Mason Barge <masonbarge@gmail.com> - 2012-06-28 13:19 -0400
      Re: Question about max() Stefan Weiss <krewecherl@gmail.com> - 2012-06-28 20:00 +0200
        Re: Question about max() Mason Barge <masonbarge@gmail.com> - 2012-06-29 10:48 -0400
          Re: Question about max() Gene Wirchenko <genew@ocis.net> - 2012-06-29 10:09 -0700
          Re: Question about max() Stefan Weiss <krewecherl@gmail.com> - 2012-06-29 21:18 +0200
  Re: Question about max() Scott Sauyet <scott.sauyet@gmail.com> - 2012-06-29 12:55 -0700
  Re: Question about max() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-30 15:42 +0200

csiph-web