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


Groups > comp.lang.javascript > #14769

Re: Question about max()

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

Show all headers | View raw


On 2012-06-29 16:48, Mason Barge wrote:
> On Thu, 28 Jun 2012 20:00:12 +0200, Stefan Weiss <krewecherl@gmail.com>
> wrote:
>>  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

> I'm just going to assume, for the time being, that it is somehow possible
> for a method to reach up into its parent object and parse the value(s) of
> a property, including an array property.

I don't think it's a good idea to assume that, because that's not at all
how it works. I'll try again:

The key is the map() function, not the callback. Another way to describe
what's happening when you write myArr.map(callbackFunc) is that the
array calls its own map() method. If you're a little familiar with
object-oriented programming, you won't be surprised that the map()
method has access to the array elements, because it "belongs" to the
array object, too (in a manner of speaking). It then iterates over the
elements and calls the callback function once for each element. Each
time, it passes three arguments to the callback function, and uses
whatever the callback returns to build a new array. At the end, it
returns the new array.

Maybe it's easier to see if we don't use an anonymous function:

  function increment(value) {
      return value + 1;
  }

  var even = [2, 4, 6, 8];
  var odd = even.map(increment);

When map() is called, it performs these steps:

  - create a new empty array:
       var result = [];

  - call the callback for the first element (index 0):
       result[0] = increment(2, 0, even);  // returns 3
       // result is now [3]

  - call the callback for the second element (index 1):
       result[1] = increment(4, 1, even);  // returns 5
       // result is now [3, 5]

  - call the callback for the third element (index 2):
       result[2] = increment(6, 2, even);  // returns 7
       // result is now [3, 5, 7]

  - call the callback for the last element (index 3):
       result[3] = increment(8, 3, even);  // returns 9
       // result is now [3, 5, 7, 9]

  - return the result

After map() returns, the result is assigned to the "odd" variable.

(In your original example, the result is not assigned to a variable.
Instead, the intermediate result (also an Array object) has its sort()
method called)

Even though map() calls increment() with three arguments, increment()
only uses the first one. In contrast with other languages, it's fine in
JS to call a function with three arguments, even if only one is listed
in the function declaration.

Hope that helps.

- 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