Mercurial > js4py-pl
annotate core-javascript/equality.rst @ 201:b6864a86e876 default tip
Przetłumaczone getting-started
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sun, 15 Jul 2012 13:27:27 +0200 |
| parents | ff23e32bbe29 |
| children |
| rev | line source |
|---|---|
| 100 | 1 Equality |
| 2 ======== | |
| 103 | 3 |
| 4 Equality in JavaScript can be a little odd. | |
| 5 | |
| 6 | |
| 7 Double equals | |
| 8 ------------- | |
| 9 | |
| 10 JavScript has two operations for equality (and inequality). One of | |
| 11 them is '==' (and '!=' for inequality). It's the oddest. | |
| 12 | |
| 13 | |
| 14 Not transitive | |
| 15 ^^^^^^^^^^^^^^ | |
| 16 | |
| 17 If *a* is equal to *b*, and if *b* is equal to *c* , then we expect | |
| 18 *a* to be equal to *c*. This is called the transitive property. | |
| 19 | |
| 20 | |
| 21 .. code-block:: javascript | |
| 22 | |
| 23 | |
| 24 js> '0' == 0 // 'a' is equal to 'b' | |
| 25 true | |
| 26 js> 0 == '' // 'b' is equal to 'c' | |
| 27 true | |
| 28 js> '0' == '' // 'a' is not equal to 'c' | |
| 29 false | |
| 30 | |
| 31 | |
| 32 | |
| 33 Not reflexive | |
| 34 ^^^^^^^^^^^^^ | |
| 35 | |
| 36 We expect *a* to be equal to *a*. This is called the reflexive | |
| 37 property. | |
| 38 | |
| 39 .. code-block:: javascript | |
| 40 | |
| 41 js> NaN == NaN | |
| 42 false | |
| 43 | |
| 44 Fortunately, this seems to be the only example. | |
| 45 | |
| 46 | |
| 47 Is symmetric | |
| 48 ^^^^^^^^^^^^ | |
| 49 | |
| 50 If *a* is equal to *b* then we expect *b* to be equal to *a*. This is | |
| 51 called the symmetric property. In JavaScript equality is symmetric. | |
| 52 | |
| 53 | |
| 54 | |
| 55 Triple equality | |
| 56 --------------- | |
| 57 | |
| 58 JavaScript also has '===' (and '!==' for inequality). | |
| 59 | |
| 60 | |
| 61 Is transitive | |
| 62 ^^^^^^^^^^^^^ | |
| 63 | |
| 64 Unlike double equals, triple equals is always transitive. | |
| 65 | |
| 66 .. code-block:: javascript | |
| 67 | |
| 68 js> '0' === 0 // unequal | |
| 69 false | |
| 70 js> 0 === '' // unequal | |
| 71 false | |
| 72 js> '0' === '' // unequal | |
| 73 false | |
| 74 | |
| 75 | |
| 76 Not reflexive | |
| 77 ^^^^^^^^^^^^^ | |
| 78 | |
| 79 .. code-block:: javascript | |
| 80 | |
| 81 js> NaN === NaN | |
| 82 false | |
| 83 | |
| 84 | |
| 85 By the way, there's a thread on Facebook with subject `Time and Date | |
| 86 on my wall shows NaNNaNNaN at NaN:NaN | |
| 87 <http://www.facebook.com/topic.php?uid=4915599711&topic=54061>`_. I | |
| 88 wonder how that happened. | |
| 89 | |
| 90 Is symmetric | |
|
130
ff23e32bbe29
Sort out table of contents, and various minor changes.
jf3243
parents:
103
diff
changeset
|
91 ^^^^^^^^^^^^ |
| 103 | 92 |
| 93 Triple equality is still symmetric. | |
| 94 | |
| 95 | |
| 96 Don't use double equality | |
| 97 ------------------------- | |
| 98 | |
| 99 Double equality does implicit conversions and besides has some odd | |
| 100 rules. My advice is **don't use double equality**. | |
| 101 | |
| 102 Triple equality does not do conversions. If you want to do | |
| 103 conversions in your comparision my advice is to make them explicit. | |
| 104 | |
| 105 | |
| 106 Compare as string | |
| 107 ----------------- | |
| 108 | |
| 109 The easiest conversion is to string. Here are two immutables, a | |
| 110 number and a string. | |
| 111 | |
| 112 .. code-block:: javascript | |
| 113 | |
| 114 js> a = 0 | |
| 115 0 | |
| 116 js> b = '0' | |
| 117 0 | |
| 118 | |
| 119 | |
| 120 These quantities are double equal but not triple equal. | |
| 121 | |
| 122 .. code-block:: javascript | |
| 123 | |
| 124 js> a == b | |
| 125 true | |
| 126 js> a === b | |
| 127 false | |
| 128 | |
| 129 Here's how to do an explicit conversion to string before comparison, | |
| 130 which gives an equality. | |
| 131 | |
| 132 .. code-block:: javascript | |
| 133 | |
| 134 js> '' + a === '' + b // Converts variables to strings. | |
| 135 true | |
| 136 | |
| 137 .. note:: | |
| 138 | |
| 139 It's always easier to read a triple equal comparison, because | |
| 140 you're not distracted by the complex double equal rules. | |
| 141 | |
| 142 | |
| 143 Compare as number | |
| 144 ----------------- | |
| 145 | |
| 146 [To follow later.] |
