Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <3335047.320Uf1SXUN@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 24 Oct 2012 21:57:02 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <722e889us0ep0mra233907giq2nnolqhge@4ax.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 90 NNTP-Posting-Date: 24 Oct 2012 21:57:02 CEST NNTP-Posting-Host: a115b9cb.newsspool3.arcor-online.net X-Trace: DXC=;4Sm?OSe_?X<<0iRN7DLEQMcF=Q^Z^V3X4Fo<]lROoRQ8kFKP40i@2K255T[N=i01>COLQQ X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:16838 Stefan Weiss wrote: > On 2012-10-24 09:35, Evertjan. wrote: >>>> Asking what OTHER languages have the same syntax as Javascript? >>>> >>>> This surely is off topic in this NG, >>>> as it does not even help understanding Javascript. > > I strongly disagree with that statement. Comparing JavaScript to other > languages is very much on topic in this group. If you're not interested > in those discussions, feel free to skip them. ACK. >> Oh sorry, perhaps you don't like this >> BECAUSE you cannot do this in some other language, >> where you would desperately need multiline if-else-then constructs. > > Just because you can write cryptic code in JS doesn't mean you have go > out of your way to do it. ACK. > Talking about cryptic code, the || operator in Perl works in a similar > way. There's even some syntactic sugar for assigning default values to > variables: > $foo = $foo || 42; > can be shortened to > $foo ||= 42; There is also $foo = 42 if not defined $foo; and similar in Perl, which is probably slightly more efficient [no assignment if $foo is not defined()/whatever]. > But enough about other languages, I don't want to bore you with these > "off topic" comparisons. Here are some better examples for where the > value returned by the || operator can be used in JS: > > // simple default values > var textColor = opts.color || userPrefs.color || "black"; There is a real use-case for that. > // DOM0 event handler > document.onclick = function (evt) { > var e = evt || window.event; > ... > }; This is error-prone if `evt' refers to a host object, and unnecessarily inefficient. And document.onclick? > // initialize a "namespace" object without overwriting it > var myLib = myLib || {}; This is unnecessarily complicated. An `if' statement will do better and will be slightly more efficient. > myLib.myFunc = function () { ... }; > > // call a method on an optional argument > function findParagraphs (parentEle) { > return (parentEle || document).getElementsByTagName("p"); > } This can be error-prone. In particular, there is a problem when expression in the `||' operation is a reference to a Function instance. Some functions can only be called as methods of an (specific) object. > // combining && and || > function textOfFirst (parentEle, eleName) { > var ele = parentEle.getElementsByTagName(eleName)[0]; > return ele && ele.firstChild && ele.firstChild.data || ""; > } This is error-prone as the return type would vary (one of Null, Element or descendant, Text, or String) depending on runtime conditions that one has virtually no control over. (In fact, the first line is error-prone already as null has no properties). To be avoided. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee