ecj2 / momo

[NOT MAINTAINED / ABANDONED] A simple 2D game-making library written in JavaScript.
MIT License
1 stars 0 forks source link

`undefined` is actually defineable #21

Closed bambams closed 6 years ago

bambams commented 6 years ago

https://github.com/ecj2/momo/blob/b74929b0ba6c208fbc587eb6edd44dcbe3b9bec2/momo.js#L6

...so you better define your own undefined as truly undefined in case somebody defines it. :x

ecj2 commented 6 years ago

I use undefined as a placeholder and update it accordingly when more information becomes available; it basically tells the browser "I've created this variable, but I don't know what it is just yet". For example, main_canvas becomes defined in Momo.setCanvas() once the ID of the canvas element is known. Before then, all that I can assume is that main_canvas will eventually be an HTMLCanvasElement object, but there is no way to initialize main_canvas to that without knowing the canvas element's ID. Regardless, the browser doesn't care either way. Nearly everything in JavaScript can be redefined, so it's moot.

That being said, if you have any suggestions or alternative ideas, or more to say on the subject, I'd be more than willing to hear them.

bambams commented 6 years ago

Using undefined is fair enough. That I don't care about. You could just as easily use null probably for this, but I think you're missing point. My memory said that window.undefined = 5 is a valid statement in JavaScript. I guess I'm an old man because it turns out that this is now read-only, but before ~2011 you could reassign a value to undefined I think. I think since this has been read-only for 7 years (if my Google fu is correct) you can reasonable close this item. That said, apparently a local variable can still be redefined so it's something to keep in mind:

(function() { var undefined = 5; return undefined; })();

You would think that undefined would be a sacred keyword, but in JavaScript, that hasn't always been the case, and still isn't.

bambams commented 6 years ago

Side note: what people often do to work around this is just declare a local variable without assigning a value. This way you can guarantee that it refers to the real undefined instead of something else. I guess the waters are muddied if the global cannot be overwritten anymore because defining a local just to ensure that it truly is undefined could just as easily be screwed up and end up assigning a different value.. So I think there's no good answer here, except for the respective committees to make undefined a keyword going forward (which they probably void doing because so many libraries that try to be compatible with old browsers explicitly define it).

ecj2 commented 6 years ago

You make a good point, but I do not think it is an issue. Even if someone redefines undefined, it would only be temporary, as Momo.setCanvas() would overwrite it. Besides, someone would only be able to redefine undefined in this context if they did so within Momo's constructor; doing so elsewhere would not work.

This is another example of how bizarre JavaScript is as a language. Surprisingly, strict mode has no quarrels with redefining undefined in the local scope. That would be the one instance where I would expect it to behave differently, but that does not seem to be the case. Additionally, undefined is supposed to be non-writable in accordance with the ECMAScript 5 specification, but not all browsers are up to snuff, it seems.

Anyway, thank you for submitting this issue (and the others); they are much appreciated. :) I am going to go ahead and close this now. Feel free to submit additional issues as you see fit though!