Hacker News new | past | comments | ask | show | jobs | submit login

> It’s not a global `print` function that’s called but `window.print`, right? `print` is sugar for `this.print` and `this` is the window object if it’s not explicitly bound to something else.

This is a common point of confusion because in a browser the global object is traditionally the same as window (or “self” or “frames”) so there wasn't a difference for many years until ES5 introduced strict mode, which left the default as undefined instead of silently using the global object, and things further fractured with Node (where it's “global”) and Web Workers (where only “self” works). (In browsers this is also complicated by the Window/WindowProxy distinction — see https://blog.whatwg.org/windowproxy-window-and-location and especially https://mathiasbynens.be/notes/globalthis#terminology)

A couple years back, “globalThis” was added to make it easier to write portable code across different environments without having to check various names: https://github.com/tc39/proposal-global

I don't think is not a particularly JavaScript-specific issue because any language which inherits scope can have the problem of a symbol not matching what you expected, which is why the ones which don't have compile-time checking tend to use linters which will report use of things which haven't been explicitly declared (not mention shadowing built-in names). If you want to blame JavaScript, the feature which would have made this more obvious would have been not ignoring extra arguments – print("foo") triggering an error at that point in the source would make it more obvious.




Wow, that’s pretty horrible.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: