Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33444
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: x === x |
| Date | 2017-05-30 16:21 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2563985.e9J7NaK4W3@PointedEars.de> (permalink) |
| References | <JavaScript-20170530123255@ram.dialup.fu-berlin.de> |
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
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