Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #26907 > unrolled thread
| Started by | JeffG <jeffgutsell@fuse.net> |
|---|---|
| First post | 2015-02-23 11:20 -0800 |
| Last post | 2015-02-23 23:26 -0300 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.javascript
puzzled by example code using the logical OR JeffG <jeffgutsell@fuse.net> - 2015-02-23 11:20 -0800
Re: puzzled by example code using the logical OR "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-02-23 21:57 +0100
Re: puzzled by example code using the logical OR Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-23 21:57 +0000
Re: puzzled by example code using the logical OR Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-23 22:10 +0000
Re: puzzled by example code using the logical OR Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-23 23:39 +0100
Re: puzzled by example code using the logical OR Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-23 23:12 +0100
Re: puzzled by example code using the logical OR Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-23 23:09 -0300
Re: puzzled by example code using the logical OR Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-23 23:26 -0300
| From | JeffG <jeffgutsell@fuse.net> |
|---|---|
| Date | 2015-02-23 11:20 -0800 |
| Subject | puzzled by example code using the logical OR |
| Message-ID | <088f6cf2-dbfe-4633-8c69-0bed2328a067@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-02-23 21:57 +0100 |
| Message-ID | <XnsA44ADF74E9D86eejj99@194.109.133.133> |
| In reply to | #26907 |
JeffG <jeffgutsell@fuse.net> 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.
Incorrect. That only happens if || compares boolean values.
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-02-23 21:57 +0000 |
| Message-ID | <87sidwi93w.fsf@bsb.me.uk> |
| In reply to | #26910 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
> JeffG <jeffgutsell@fuse.net> 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.
>
> Incorrect. That only happens if || compares boolean values.
>
> 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.)
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-02-23 22:10 +0000 |
| Message-ID | <mcg8ga$gs7$1@dont-email.me> |
| In reply to | #26913 |
On Mon, 23 Feb 2015 21:57:55 +0000, Ben Bacarisse wrote:
> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>
>> JeffG <jeffgutsell@fuse.net> 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.
>>
>> Incorrect. That only happens if || compares boolean values.
>>
>> 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.)
More generally, when comparing multiple (2 or more) values with ||, from
left to right, the first "not considered false"[1] value is returned, or
false if all the values being compared are "considered false"[2].
[1] sometimes referred to as truthy
[2] sometimes referred to as falsey
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-02-23 23:39 +0100 |
| Message-ID | <1584643.PrhRbllIxL@PointedEars.de> |
| In reply to | #26914 |
Denis McMahon wrote:
> On Mon, 23 Feb 2015 21:57:55 +0000, Ben Bacarisse wrote:
>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>>> 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.)
>
> More generally, when comparing multiple (2 or more) values with ||,
Again, “||” is _not_ a comparison operator; it is a *binary* *logical*
operator (that takes exactly *2* operands, hence “binary”). “x || y || z”
is equivalent to “(x || y) || z”, “a || b || c || d” is equivalent to
“((a || b) || c) || d”, and so forth:
| LogicalORExpression :
| LogicalANDExpression
| LogicalORExpression || LogicalANDExpression
> from left to right, the first "not considered false"[1] value is returned,
> or false if all the values being compared are "considered false"[2].
No; it follows from the grammar and the algorithm: if all operands up to the
second-last one in a chain of “||” operations (see above) yield results that
are converted (by an implementation of the ToBoolean algorithm) to “false”,
then the result of the expression is the result of evaluating the last
operand:
var answer = 0 || +"y" || false || 42;
var zero = void 0 || +"z" || false || 0;
Once again: If you are not certain, do not post. *Nobody* is being helped
by such half-wit guesses and terminology. Read the pertinent Specification,
in this case <http://ecma-international.org/ecma-262/5.1/#sec-11.11>, and
run some *proper* tests based on that before you post. TIA.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-02-23 23:12 +0100 |
| Message-ID | <2611492.d6m9CFKcER@PointedEars.de> |
| In reply to | #26913 |
Ben Bacarisse wrote:
> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>
>> JeffG <jeffgutsell@fuse.net> 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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-02-23 23:09 -0300 |
| Message-ID | <mcgmh2$99b$1@speranza.aioe.org> |
| In reply to | #26907 |
On 02/23/2015 04:20 PM, JeffG wrote:
> 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.
>
According to the ES 5.1, §11.11 (Binary Logical Operators), "The value
produced by a && or || operator is not necessarily of type Boolean. The
value produced will always be the value of one of the two operand
expressions".
<http://ecma-international.org/ecma-262/5.1/#sec-11.11>
Also, as described in ES 5.1, §9.2 (ToBoolean), there are seven "falsy"
values: false, +0, -0, "", NaN, null, and undefined. All other values
are "truthy".
<http://ecma-international.org/ecma-262/5.1/#sec-9.2>
So, AppSpace || {} is evaluated as follows:
If ToBoolean(AppSpace) is true, return AppSpace; otherwise, return {}
(an empty object).
But it is a tad efficient to use:
var AppSpace;
if (!AppSpace) {
AppSpace = {};
}
Or simply:
var AppSpace;
if (!AppSpace) AppSpace = {};
The logical operators may be described in the following table:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators>
--
Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-02-23 23:26 -0300 |
| Message-ID | <mcgngj$b1t$1@speranza.aioe.org> |
| In reply to | #26921 |
On 02/23/2015 11:09 PM, Joao Rodrigues wrote:
> On 02/23/2015 04:20 PM, JeffG wrote:
>> 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.
>>
>
> According to the ES 5.1, §11.11 (Binary Logical Operators), "The value
> produced by a && or || operator is not necessarily of type Boolean. The
> value produced will always be the value of one of the two operand
> expressions".
> <http://ecma-international.org/ecma-262/5.1/#sec-11.11>
>
> Also, as described in ES 5.1, §9.2 (ToBoolean), there are seven "falsy"
> values: false, +0, -0, "", NaN, null, and undefined. All other values
> are "truthy".
> <http://ecma-international.org/ecma-262/5.1/#sec-9.2>
>
> So, AppSpace || {} is evaluated as follows:
>
> If ToBoolean(AppSpace) is true, return AppSpace; otherwise, return {}
> (an empty object).
>
> But it is a tad efficient to use:
Better: a little bit more efficient.
>
> var AppSpace;
> if (!AppSpace) {
> AppSpace = {};
> }
>
> Or simply:
>
> var AppSpace;
> if (!AppSpace) AppSpace = {};
>
Sorry, it should be:
if (typeof AppSpace == "undefined") {
var AppSpace = {};
}
>
> The logical operators may be described in the following table:
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators>
>
>
>
--
Joao Rodrigues
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web