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


Groups > comp.lang.javascript > #14785

Re: Question about max()

Message-ID <1917299.FpnORuNF1W@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-06-30 15:42 +0200
Subject Re: Question about max()
Newsgroups comp.lang.javascript
Followup-To comp.lang.javascript
References <njnou7tungmkib8ip6tbbq7oqufah4d027@4ax.com>

Followups directed to: comp.lang.javascript

Show all headers | View raw


[All good things …]

Mason Barge wrote:

> I encountered the following line of code in a lesson from a "learn
> javascript" tbook:

Apparently not a good book if you are asking about basics here.  Or is the 
student the problem?
 
> **
> var sorted = words.map (function(value) {return
> value.toLowerCase();}).sort();
> **
> 
> I get all of this except for one thing.  How or from where does the
> parameter of the anonymous function get its argument?

The question you wanted to ask was "How or from where does the parameter of 
the anonymous function get its value?"  (A function has *parameters*.  The 
values passed to it are the function's *arguments*; they may be assigned to 
declared parameters.  [FTR: I did get the "parameters" part wrong until not 
so long ago, but the 5.1 Edition of ECMAScript – at least – is definitive 
about the terminology there.)

The correct answer to that question is "The parameter gets its value from 
the argument that is passed to the anonymous function when that function is 
called by map().

Think of map() being defined as follows, which should be self-explanatory 
(the algorithm is specified in ECMAScript, this is a possible implementation 
of it, from JSX:object.js):

  Array.prototype.map = function (callback, oThis) {
    var jsx_object = jsx.object;
      
    if (!jsx_object.isMethod(callback))
    {
      jsx.throwThis("TypeError",
          (jsx_object.isMethod(callback, "toSource")
          ? callback.toSource()
            : callback)
          + " is not callable",
        this + ".map");
    }
        
    var
      len = this.length >>> 0,
      res = [];
          
    for (var i = 0; i < len; i++)
    {
      res[i] = callback.call(oThis, this[i], i, this);
    }
      
    return res;
  };

Array instances inherit from Array.prototype through the prototype chain, 
and `words' is an Array instance.  So it inherits the map() method from 
Array.prototype, too.

The anonymous function passed to map()¹ is defined *before* but called *by 
map()* *later*.  That is,

  var sorted = words.map(function (value) {
    return value.toLowerCase();
  }).sort();

is functionally equivalent to

  var foo = function (value) {
    return value.toLowerCase();
  };

  var sorted = words.map(foo).sort();

This pattern is called a *callback*, which originates in functional 
programming and is very useful (cf. currying, event listeners).  ECMAScript 
implementations are one of the more interesting programming languages 
because they combine several programming paradigms: procedural-imperative, 
object-oriented and functional programming.  (This can also be found in PHP 
and Python, and to a lesser extent in Java.)
 
> There is no mention of a "var value"

Why should it?  The parameter of the function is declared in the formal 
parameter list already; the declared name is `value'.

> and by experimentation any variable name works,

_Not_ *any* name; it must be an *identifier*.  That aside: Of course.  
Functions often have parameters because they are written to avoid repetitive 
programming of similar tasks.  How a function parameter is named is of 
secondary importance, as it is *local* to the function code only.  
Programming 101.

> so it has nothing to do with the frequently encountered property "value"

Correct.

> (and afaik using the word "value" for the parameter was
> not a good choice of words, even though it isn't reserved).

Only if it is not the only named parameter.  In this case, the value of the 
array element is passed, so `value' certainly is an appropriate name.  
However, if this bothers you, you may name the parameter `element'.  You may 
also name it `supercalifragilisticexpialigocious' as long as you use the 
same identifier with the same meaning in the function code, but that would 
be harder to maintain and a bit silly on top of that.


PointedEars
___________
¹  For simplicity, I am saying "function" here when it would be exact to say
   "reference to a Function instance" etc.  Objects have identity, not name.
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Back to comp.lang.javascript | Previous | Next — Previous 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