Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.85.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Is JS a functional programming language? (was: Trying to create array of object literals) Date: Sat, 21 Apr 2018 02:35:02 +0200 Organization: PointedEars Software (PES) Lines: 87 Message-ID: <1703120.hZdRbIOfhN@PointedEars.de> References: <2ea2803e-c39d-40e0-b8ed-136c3dc9b4e0@googlegroups.com> <3364120.8dbtfUO1ph@PointedEars.de> <4mv0ddt3bre9bbiofnvabnbamlp9jfj072@4ax.com> <15901681.8V3EAehsKz@PointedEars.de> <3dd0b784-9634-4047-b79b-a171d1e1d099@googlegroups.com> <611cba1f-e8f1-4ca1-8eb5-5e10456200d1@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="85.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.236.85"; logging-data="23000"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-User-ID: U2FsdGVkX19G6iDx5WsRxivTxtNlkRChewAWPnA9IMav2/QrYjCrmg== Cancel-Lock: sha1:erJLUyow9/jLilfY1xsTCIJR/FU= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Julio Di Egidio babbled: >> JS is a *functional* language: the modularity is modularity of *lexical* >> constructs and contexts... (Someone else might be able to say it >> better.) Modularity is not the key property of a functional programming language. > Yes, JavaScript is a "functional" language. Here is an example: > > class Moron { > constructor(name) { > this.name = name; > } > speak() { > alert(`${this.name} says: > "JS is a functional language."`); > } > } Note that the “speak” method of the “Moron” class is implemented as an *object*, a *Function* *instance*. Note also that this does not work in several earlier implementations of ECMAScript, where you have to write function Moron (name) { this.name = name; } Moron.prototype.speak = function () { window.alert(this.name + ' says: "JS is not a functional programming language."'); }; (new Moron('The troll posting as "c.l.js"')).speak(); or something to that effect, instead. > var Julio = new Moron('Julio'); > Julio.speak(); [It is the standing recommendation that only class, constructor, and constant identifiers should start with a capital letter (after leading “_”s are trimmed. “Julio” is an *instance* and a *variable*; the identifier should start lowercase.] Your unnecessary insult aside: Functional programming is a programming *paradigm*. A functional programming language is a programming language where functions are first- class objects: they can appear on the right-hand side of an assignment expression and/or they can be passed as arguments to other functions. *Functional programming is _not_, in general, a contradiction to object- oriented programming.* Proof that JS is/are (a) functional programming language(s) has been given implicitly above. Another example: var f = Julio.speak; (function (func) { func.call({name: "Thomas ‘PointedEars’ Lahn"}); }(f)); To be more precise, implementations of ECMAScript, such as the languages that have “JavaScript” in their name, are procedural-imperative, functional and object-oriented programming languages that support both the prototype- based and (to some extent) class-based inheritance programming paradigm. *That* they support *all* of *those* programming paradigms, in *addition* to others, is an important reason for their versatility and wide distribution. See also the FAQ (referred below). -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.