You could opt in to an edition by adding a JavaScript directive at the start of the file — like the "use strict" directive. e.g.
```js
"use edition2023"
```
This could enable to remove obsolete features and improve strictness.EDIT: I forgot the "use" before "strict".
You write Typescript or CoffeeScript or Clojurescript or ES6 or in this case "JS edition2023". You have a preprocessing step that compiles this into code that works on everyone's browser. Your new language has the features and quirks you want, without having to solve the problem of deploying a new runtime to a billion older machines.
But yeah, as a preprocessing step, I'd argue a linter rule would be one of the best way to handle such a quirk if you are using bare JS (or TypeScript, which inherits this quirk, by the way)
For instance, we could freeze class prototypes making impossible to dynamically replace a method or adding a method to it.
This what "use strict" did by forbidding `with` statements for example.
No need to fork the language for this!
"use strict" was for fixing some fundamental mistakes which couldn't be fixed in a backwards-compatible way.
“Use strict” was necessary because it changed runtime semantics in a non-backwards compatible way. But browser vendors does not like to support multiple incompatible modes, it is much preferrable to introduce improvents in a backwards-compatible manner.
I don't think it can be a complete solution for making typeof return "null" for null for two reasons.
1) If you call f(typeof v) where f is defined in a module written in a different version, you are screwed. Contrived but within spec and we all know https://xkcd.com/1172/ (if you didn't, now you do).
2) Moreover, old engines will ignore your directive and behave differently (return "object" instead of "null"), so now your code is incorrect depending on what engine runs it.
In the end, it could as well be a linter rule that forces you to test v === null before calling typeof v and I think it would be the best option. If you care about this kind of stuff, you are probably already using a linter. The same kind of tool that complains about "==", which sets the same kind of traps, and you don't need to wait for something wrong to happen during execution, it's already warned statically before even running the code.
If you use a function declared in a module with a distinct edition, you have to comply to its contract. And its contract certainly state that "null" is not a valid value to pass through.
Moreover, a type checker or linter could error/warn about this.
(2) You are right.
Another solution might be to introduce a new keyword:
```js
use "edition2023"
```
Old browser could thus crash. In the same way that they did for ESM. In the end, it could as well be a linter rule that forces you to test v === null
before calling typeof v and I think it would be the best option. If you care
about this kind of stuff, you are probably already using a linter.
Yes. However, this does not allow performing more advanced optimizations. Bundlers, Transpilerss minifiers, and compilers must handle all this complexity making some optimizations unsafe or even impossible.Maybe it was a mistake to introduce "use strict" without any other identifier, we need "use strict" with a version to be able to deprecate things and eventually remove from JS. Then JS engine could read it in the beginning of the file and know which version of js it is. Because in current JS we can only add new features without removing them. I am sure it is very complicated problem since we don't want to break the internet and browsers still have to work with old websites.
if (typeof varname === "object" && !varname) { loggingNull("varname"); }
it seems far fetched and maybe those people's code should break.
I guess it doesn't really affect me though.
PL design is complicated and you pretty much have to keep this kind of quirks forever or at least for a very long time if you want to avoid a painful transition, which can lead to the old language version hanging out forever in the wild because a lot of code is already written in it. You can deprecate the features the quirks affect and introduce new ones to replace them that do not have the quirks, but now you need to support both ways for a very long time until the ecosystem is ready, which may never happen.
there's nothing wrong with:
switch (typeof v) {
...
case ...:
...
break;
case "undefined":
// handle undefined;
break;
case "object":
if (!v) {
// handle null;
} else if (v instanceof 'Array') {
// handle array;
} else if (...) {
...
} else {
// handle anything else
}
break;
}
that's within spec and an alternative could be more verbose / more difficult to navigate (it's a matter of taste). If you need to handle undefined and null differently, you need to test for it and typeof === "object" is one way. I would probably do it differently, but I can see someone thinking differently and we can't blame them.And some sites will never be updated because they are not actively maintained.
-said by someone last millenniumfrom the hyrum's law link:
With a sufficient number of users of an API,
it does not matter what you promise in the contract:
all observable behaviors of your system
will be depended on by somebody.