Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: puzzled by example code using the logical OR Date: Mon, 23 Feb 2015 23:12:07 +0100 Organization: PointedEars Software (PES) Lines: 63 Message-ID: <2611492.d6m9CFKcER@PointedEars.de> References: <088f6cf2-dbfe-4633-8c69-0bed2328a067@googlegroups.com> <87sidwi93w.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1424729607 10695 eJwFwQcBACAIBMBKPMiKo4z+EbxTMVj5MbWjqxtLzjKyF1ozog8LvtTZkzJRSw/p3UHMkPgl1hEr (23 Feb 2015 22:13:27 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 23 Feb 2015 22:13:27 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNxMEBwCAIA8CVDELEccDo/iO097iYBM9yBj1evKFuWxJn6f1ZyrdFHhTEEXBzFq5tVM0rw27LJrKetOIDVuIVew== Cancel-Lock: sha1:C9LFDE4RZ5cUuRTFUMclyf5Zk3Y= X-NNTP-Posting-Host: eJwFwYERACEIBLGW8DkWLEcZ6b+ET8JZdIpAMTFSujVZ9g07j1QwdNHrlDa3D8a7bvFqvR/9PhBV Xref: csiph.com comp.lang.javascript:26915 Ben Bacarisse wrote: > "Evertjan." writes: > >> JeffG wrote on 23 feb 2015 in >> comp.lang.javascript: >> >>> I've been looking at a discussion of creating objects that >> are >>> containers of all related functions and vars. An example >> first step is >>> as follows: ar AppSpace = AppSpace || {}; >>> I don't see the purpose of "||". I would appreciate an >> explanation since >>> I cannot get a response from the author. I thought the >> logical OR only >>> returns true or false based on comparing two values or >> expressions. This quotation (sic!) increases reader’s risk for getting eye cancer considerably. Whoever caused it should LART themselves and get a working newsreader before their next posting. >> Incorrect. That only happens if || compares boolean values. Nonsense. “||” is not a comparison operator to begin with. >> AppSpace = AppSpace || {}; >> >> AppSpace is set to AppSpace [so effectivly remains so], >> unless AppSpace returns false or empty or 0 or undefined, >> then AppSpace is set to {}, an empty object. > > It's false, the empty string, 0 (both +0 and -0), NaNs, undefined and > null that are considered false. (It's common not to care about -0 and > NaNs, but null matters a bit more.) Bottom line: The intention of this is that “AppSpace” is assigned a reference to a new Object instance if, and only if, that has not been done before. The pattern is questionable, and I try to avoid it, because reading a property needs not yield the same value as writing it, host objects may not be written to (people who use this do not know the difference), and one assignment there is unnecessary: if (!AppSpace) { AppSpace = {}; } is not much longer, but it is more obvious, less error-prone, and more efficient in the worst case. It also has the advantage that, should it be necessary to adapt the condition for assignment (for example, using “typeof”), it is easier to do modify accordingly. BTW, “return” is not the proper term for this; no function is called that could return anything, and the relevant Specification’s algorithms (which do return values) require a different, more elaborate wording to be referred to correctly. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.