Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33444

Re: x === x

Path csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.albasani.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: x === x
Date Tue, 30 May 2017 16:21:13 +0200
Organization PointedEars Software (PES)
Lines 144
Message-ID <2563985.e9J7NaK4W3@PointedEars.de> (permalink)
References <JavaScript-20170530123255@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
NNTP-Posting-Host 47.229.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace gwaiyur.mb-net.net 1496154074 15189 178.197.229.47 (30 May 2017 14:21:14 GMT)
X-Complaints-To abuse@open-news-network.org
NNTP-Posting-Date Tue, 30 May 2017 14:21:14 +0000 (UTC)
User-Agent KNode/4.14.2
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Xref csiph.com comp.lang.javascript:33444

Show key headers only | View raw


Stefan Ram wrote:

>   In mathematics, usually, when »...« is a certain term, we have
> 
> ... === ...
> 
>   for the same term left and right.
> 
>   In JavaScript, this does not always hold for expressions, albeit
>   possibly sometimes for different reasons:

Just “possibly” – are you serious?  You profess to teach, but apparently 
still do not understand the basics of, these programming *languages*; nor, 
it appears, programming in general.
 
> Math.random() === Math.random()

This should be obvious even to you: The return values of consecutive calls 
of Math.random() may be, but are not supposed to be, equal.  After all, the 
method is supposed to generate *random* numbers.

> NaN === NaN

This evaluating to “false” is due to the implementation of the ECMAScript 
Number type as a type of IEEE 754 “double-precision” floating-point values.

> [] === []

Left-hand side and right-hand side refer to different, consecutively 
constructed, Array instances, and the operator compares the reference 
values, not the syntax or object content.

    [To do the latter, Array.prototype.every() may be used:

       [1, 2, 3].every(function (e, i) { return this[i] === e }, [1, 2, 3])

     Note that arrow functions must not be used then, as “this” would not 
     be defined by the second argument then:

       [1, 2, 3].every((e, i) => (
         /* Does not work: “this” is “undefined” in strict mode
                           or refers to the global object */
        this[i] === e
       ), [1, 2, 3])

     That is IMHO an oversight in the Specification that precludes the use
     of arrow functions where they would be most useful for short syntax.]

> ( {} === {} )

Left-hand side and right-hand side refer to different, consecutively 
constructed, Object instances, and the operator compares the reference 
values, not the syntax or object content.

    [To do the latter, a custom equals() method may be defined, like in 
     Java.  Care must be taken to avoid augmenting the Object prototype; 
     otherwise, for-in and for-of loops must be filtered, and the “in” 
     operator either cannot be used in a simple manner, or can only be 
     applied to objects that do not have the Object prototype in their 
     prototype chain, such as those created with Object.create(null, …).

     The introduction of an Object.equals() method that compares object
     content, by calling the first referred object’s equals() method 
     unless the object is the same as referred by “Object” and if 
     such a method is defined, using simple comparison otherwise, is
     indicated:

       Object.equals = function (obj1, obj2, options) {
         if (obj1 != Object && typeof obj1.equals == "function")
         {
           return obj1.equals(obj2);
         }

         return (
           options && options.enumerableOnly
             ? Object.keys(obj1)
             : Object.getOwnPropertyNames(obj1)
         ).every(
           options && options.strict
             ? function (key) { return (obj1[key] === obj2[key]); }
             : function (key) { return (obj1[key] == obj2[key]); }
         );
       };

    ]

> ( ()=>0 )===( ()=>0 )

Left-hand side and right-hand side refer to different Function instances, 
and the operator compares the reference values, not the syntax or object 
content.

    [There is no reliable *and* *normatively* backwards-compatible way to do 
     the latter, however the string representations of both functions may be 
     compared in newer implementations.  See ECMAScript 2016, § 19.2.3.5 
     Function.prototype.toString(), “toString Representation Requirements”.]

>   In the first case, we cannot be sure that it will evaluate to
>   »false«, but this is is possibly.
                 ^^^^^^^^^^^^^^^^^^^
Parse error.
 
>   The last three cases are similar, but it is also remarkable
>   how one needs to add parentheses: no parens required for the
>   array, outer parens for the objects,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No parentheses are required there; the “===” operator marks “{}” as an 
expression as opposed to an empty Block statement.

  {} === {}

is equivalent.

Besides, arrays *are* objects in ECMAScript implementations.  The proper 
terms to use there to make the distinction are “Object initializer” to refer 
to the syntax element, and “Object instance reference” to refer to the 
evaluation value.

>   and two pairs of parens for the functions.

The parentheses there are required to satisfy the grammar and to avoid 
ambiguity:

The parentheses around the second arrow function are required to avoid 
defining an arrow function that attempts to compares 0 with a syntactically 
invalid expression, “()”, equivalent to

  () => ((0 === ()) => 0)
-----------------^

    [“=>” is not an operator; the greater-than-or-equal operator is “>=”. 
     Therefore, “0 === ()” is the longest possible match.]

The parentheses around the first arrow function are required to avoid the 
ambiguity of defining an arrow function that compares 0 and a Function 
instance reference created by the second arrow function, equivalent to

  () => (0 === (() => 0))

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: x === x Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-30 16:21 +0200
  Re: x === x John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-30 19:16 +0100
  Re: x === x Dr J R Stockton <J.R.Stockton@physics.org> - 2017-05-31 08:39 -0700
    Re: x === x Stefan Weiss <krewecherl@gmail.com> - 2017-05-31 18:08 +0200
      Re: x === x Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-06-02 22:27 +0100
    Re: x === x Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-31 21:03 +0200
      Re: x === x Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-06-02 22:12 +0100
    Re: x === x Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-31 21:09 +0200
      Re: x === x Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-01 01:58 +0100
        Re: x === x Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-06-01 05:01 +0200
          Re: x === x Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-01 14:44 +0100
          Re: x === x Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-06-02 21:15 +0100
        Re: x === x Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-01 14:17 +0100
    Re: x === x Julio Di Egidio <julio@diegidio.name> - 2017-06-01 00:10 -0700
      Re: x === x Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-06-02 22:16 +0100

csiph-web