“As much as I’d love our code to be fully compliant with the great programming practices of this world, I’d rather have it working — if possible before the heat death of the Universe.”
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
“As much as I’d love our code to be fully compliant with the great programming practices of this world, I’d rather have it working — if possible before the heat death of the Universe.”
Oops. That actually explains a lot, as I used to get could not read property 'foo' of undefined a lot when passing my functions around as references, in strict mode.
Thank you for the link, I had forgot how much use strict mode changes the game!
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Why does it happen though, that this code ran on Firefox (and I suppose, any other browser):
prints respectively the
Window
object and theobj
one?Great question. You are getting Window because you are not using 'use strict'.
Go to this link and the section called "Securing JavaScript" explains it.
developer.mozilla.org/en-US/docs/W...
Oops. That actually explains a lot, as I used to get
could not read property 'foo' of undefined
a lot when passing my functions around as references, in strict mode.Thank you for the link, I had forgot how much
use strict mode
changes the game!It makes code a little bit more reasonable.
Babel has implicit strict mode and u don't have to worry about forgetting to add it. :)
Without specific definition,
this
is the object wrapping the function.window
is the fallback when can't be infer from any context.To get undefined as result you have to use the strict mode.