denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.75k stars 2.55k forks source link

Weird (wrong?) parentheses in example code #175

Closed fireattack closed 3 years ago

fireattack commented 3 years ago

Lots of examples in the article have weird parentheses in it and I'm not sure what they are for.

Example: https://github.com/denysdovhan/wtfjs#precision-of-01--02

0.1 +
  0.2(
    // -> 0.30000000000000004
    0.1 + 0.2
  ) ===
  0.3; // -> false

What's the purpose of parentheses here? It doesn't generate valid code (Uncaught TypeError: 0.2 is not a function at <anonymous>:2:6), nor make it easier to read (compared to just

0.1 + 0.2;   // -> 0.30000000000000004
0.1 + 0.2 === 0.3; // -> false

). I read the "notation" at the beginning and didn't find anything relevant either.

There are plenty of others, e.g.

https://github.com/denysdovhan/wtfjs#patching-numbers

Number.prototype.isOne = function() {
  return Number(this) === 1;
};

(1.0).isOne(); // -> true
(1).isOne(); // -> true
(2.0)
  .isOne()(
    // -> false
    7
  )
  .isOne(); // -> false

.. and so on.

libook commented 3 years ago

It looks like a mistake from merging code. Checkout this https://github.com/denysdovhan/wtfjs/tree/c2b38a183a6c13c8ceb44eb1e55238d17875e95c#precision-of-01--02 The original version should be like you written:

0.1 + 0.2 // -> 0.30000000000000004
(0.1 + 0.2) === 0.3 // -> false

You can submit Pull Requests.

fireattack commented 3 years ago

It looks to me someone ran a formatter with all the code and messed up lots of things (see also "Destructuring with default values" etc.) I will see if I can fix it as much as I can.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.13.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: