News for hackers. Stuff that matters.
Re:WTFJS – a list of funny and tricky JavaScript examples by uwu on Monday July 31, 2017 @09:18AM (#14891625)
> Object.create(Array).length === 1 //true

it bothers me how the examples are often misleading or just misusing language features but people who don't know the language will gladly agree with them and have their "js sucks" belief reinforced

Re:WTFJS – a list of funny and tricky JavaScript examples by twiss on Monday July 31, 2017 @09:43AM (#14891850)
To those curious what it does, Object.create(Array) creates an object with the Array constructor function (not the Array prototype) as its prototype. The Array constructor function, like all functions, has a length property indicating the number of arguments it takes. That property is accessed through the created object's prototype chain.

The Array constructor's length property is defined to be 1, referring to the fact that if you call it with 1 number, you get an array of that length filled with undefineds. If you call it with something else or another number of arguments, you get an array filled with the arguments.

Re:WTFJS – a list of funny and tricky JavaScript examples by Sean1708 on Monday July 31, 2017 @09:40AM (#14891832)
What's the reasoning behind this particular example?
Re:WTFJS – a list of funny and tricky JavaScript examples by uwu on Monday July 31, 2017 @09:54AM (#14891953)
i actually got it from https://wtfjs.com/ which was linked in another comment

someone explained how it works here: https://news.ycombinator.com/item?id=14891850

Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @09:31AM (#14891744)
Agreed, although some things like:

  NaN === NaN // -> false 
Are pretty f*&^%$ up, no matter how you look at them

Edit: well, apparently there is a non f&^%$ up way to look at it: https://news.ycombinator.com/item?id=14891810

Re:WTFJS – a list of funny and tricky JavaScript examples by xyclos on Monday July 31, 2017 @09:38AM (#14891810)
NaN is meant to represent a non-sensical mathematical operation (like divide by 0). One non-sensical mathematical operation is not the same as some other non-sensical mathematical operation (1/0 !== Infinity/Infinity)
Re:WTFJS – a list of funny and tricky JavaScript examples by Pigo on Monday July 31, 2017 @09:44AM (#14891862)
That's a good point, a lot of this seems like it can be explained with some context or just asking why you'd ever want to do that.
Re:WTFJS – a list of funny and tricky JavaScript examples by gmiller123456 on Monday July 31, 2017 @04:43PM (#14895731)
>NaN is meant to represent a non-sensical mathematical operation

This is not a very good explanation. NaN is used to represent a nonsensical value. By your explanation (1/0 === 1/0) should be true since they represent the same invalid opperation. The fact that NaN != NaN is not meant to mean anything, it is merely defined that way to prevent a class of bugs from occurring. Anyone not familiar with the definition is correct to be confused by it. I'd think if the spec was designed today, this special case (hack) wouldn't be there and we'd have exceptions in its place.

Re:WTFJS – a list of funny and tricky JavaScript examples by justinpombrio on Monday July 31, 2017 @11:06AM (#14892663)
Nor is it the same as the same non-sensical mathematical operator :-)

1/0 !== 1/0

Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @11:46AM (#14893019)
That's mad. In JS, 1/0 is Infinity, not Undefined. And Infinity is not equal to itself.
Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @10:09AM (#14892106)
That's a good explanation, thanks.
Re:WTFJS – a list of funny and tricky JavaScript examples by roywiggins on Monday July 31, 2017 @09:35AM (#14891785)
That's just normal floating point behavior. NaN isn't equal to anything, not even itself. It's in the standard.
Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @10:09AM (#14892103)
Floating point can be a bit mind bending. Still, something not being equal to itself almost pushes the barrier into philosophy.
Re:WTFJS – a list of funny and tricky JavaScript examples by roywiggins on Monday July 31, 2017 @01:30PM (#14894106)
As long as you can define what's going on rigorously, it's just mathematics :) The equality operator means just what I choose it to mean- neither more nor less, as Humpty Dumpty would say.

Also fun: (NaN < x) should be false for every x, and (NaN > x) should be false for every x. It's not a number, so it's not less than or greater than any number. But then by process of elimination, NaN == x for every x. Which is obviously nonsense. So equality and comparison is completely "broken" anyway when you start comparing NaN to things.

Re:WTFJS – a list of funny and tricky JavaScript examples by fmihaila on Monday July 31, 2017 @10:54AM (#14892532)
NaN is not a name for one thing. You could think of not a number as not any particular thing. There is no comparison with itself, because there is no it.
Re:WTFJS – a list of funny and tricky JavaScript examples by goatlover on Monday July 31, 2017 @11:02AM (#14892625)
How is isNaN() implemented?
Re:WTFJS – a list of funny and tricky JavaScript examples by Gaelan on Monday July 31, 2017 @11:21AM (#14892804)

    function isNaN(x) { return x !== x }
It’s a native browser function so it may not actually be done that way, but that’s one way you could do it.
Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @11:37AM (#14892940)
We really are deep in the realm of Philosophy.
Re:WTFJS – a list of funny and tricky JavaScript examples by fny on Monday July 31, 2017 @10:25AM (#14892248)
There's a really great SO on why this is an IEEE standard: https://stackoverflow.com/questions/1565164/what-is-the-rati...
Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @11:35AM (#14892924)
Great discussion indeed. I was thinking along the same lines, the comparison may be meaningless, but “false” is not a good answer either, even though a Boolean is either true or false by definition and NotABool would be even more crazy. What a rabbit hole.
Re:WTFJS – a list of funny and tricky JavaScript examples by aisofteng on Monday July 31, 2017 @09:36AM (#14891788)
That's part of IEEE 754.
Re:WTFJS – a list of funny and tricky JavaScript examples by tambourine_man on Monday July 31, 2017 @10:05AM (#14892068)
I didn't know it was part of IEEE 754, thanks. So JS is not alone in this.