Created
March 5, 2018 18:47
-
-
Save jacopotarantino/cbe310d0042527cddc497ce9c709e7a3 to your computer and use it in GitHub Desktop.
Impossible JavaScript Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* what do each of these statements return? | |
(assume that they are evaluated individually but in order.) | |
*/ | |
const foo = 'asdf'; //=> ? | |
foo.bar = 12345; //=> ? | |
foo; //=> ? | |
foo.bar; //=> ? | |
Object.getOwnPropertyNames(foo); //=> ? |
I didn't realise the second one behaved differently without strict mode. Are we assuming non-strict?
What's the use of knowing this? You wouldn't do it in production so who cares?
@EionRobb - I hadn't even thought about strict mode yet. Good catch!
@mgd020 - Shits and giggles lol. This actually came up because of production code so somebody did it in production.
@gkaykck - Haha. Well done ;). I absolutely don't expect people to know this stuff by heart. That's why it's such a weird challenge. This isn't stuff that you should know because you should never be doing this nonsense to begin with.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hehe. This is actually derivative of some code I found that took me and another programmer some time to split up and figure out what it was doing and if that's even possible. What really got me is that the assigning to a property of a string seems like it should throw an error but it doesn't (in most JS environments). So we turned it into a reduced test case and started asking people to solve it. It's not really for proving that people don't know JS or making a huge statement about the language. It's an illustration of what happens when you write really weird/bad code. As an extra bit of fun, it's a nifty way to pry at how people thing the language does and should work.