A Perplexing JavaScript Parsing Puzzle
hillelwayne.com
hillelwayne.com
x
= 1
x
--> 0
which does not execute. And if I run them as two lines:
x = 1
x --> 0
then the final output is `true`. `1` is only output during the initial assignment, which is hardly surprising
If i understand correctly, the reason i am getting `true` is because the second line gets parsed as two operations:
(x--) > 0
first a post-decrement operator (--) followed by a comparison (greater than). Since x = 1 it's greater than 0 (true), but afterward it's decremented so you'll get false if you run the same line again
John.constructor("alert('hello')")()
JS has been around a long time. Lots of old stuff hangs around.
Yes, it pops an alert, and no it does not console log.
John.constructor === Function
John !== Function
John is (approximately) a reference to a desugaring of its `constructor` “method” (which is the term I see used on MDN, I would have expected something more akin to “constructor declaration”; not 100% sure what the spec terminology is and don’t have time to chase it down atm) + its instance members assigned to the desugared function’s prototype. It’s more complicated than that for… reasons.But the gist is that John.constructor references something other than the constructor defined in the class body, so calling that reference doesn’t call the thing it doesn’t reference at all.