Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news-peer.in.tum.de!news.belwue.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1917299.FpnORuNF1W@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 30 Jun 2012 15:42:04 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Supersedes: <2366683.1XLLoLTjXO@PointedEars.de> MIME-Version: 1.0 Lines: 121 NNTP-Posting-Date: 30 Jun 2012 15:42:04 CEST NNTP-Posting-Host: 620d7b66.newsspool3.arcor-online.net X-Trace: DXC=92RaUmDX``=_A0jCfgHO6>McF=Q^Z^V384Fo<]lROoR18kF 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,