|
|
|
|
|
by jrochkind1
4181 days ago
|
|
I understand the implementation details, but the fact remains that previously you could call `typeof something` for any `something` at all, and it would never throw, ever. So you could use it as a way to check if a variable was defined. Now, sometimes it will throw. I mean, come on, you really don't see anything confusing in the fact that there are now two kinds of `undefined`, one kind you can call `typeof x` and it returns `undefined`, but another kind of undefined where you can't mention x at all without a throw? Like, if you can't mention undefined variables without getting a throw, how come `typeof` sometimes returns `undefined`? Ah, because some "undefined" variables you can do that with, but not others? So I guess there is more than one "kind" of "undefined" now? This is not confusing? |
|
No, typeof /isn't/ throwing.
This will throw the same exception for exactly the same reason. You're using a variable defined using "let" before the variable definition. Sod all to do with typeof.I don't see it as massively confusing that a variable declared one way behaves differently to a variable declared another way. That's the whole point of having a new way of declaring a variable.
Why isn't everyone up in arms about how confusing it is that it behaves differently here?
Oh no, if I use "let" it behaves differently to "var"!Its not like its throwing on unexpected data, its throwing because the program isn't constructed properly. This is the same as just about every other language.
Are you really saying we shouldn't make improvements to the language because then it would behave differently to the old, poorly designed features we're trying to deprecate?
[edit] You still can use typeof to check if something is defined without throwing on spurious inputs. The only time the code will throw is if you make changes to further down in the lexical scope. And guess what - whether you do that with var or with let you've changed the argument of typeof to refer to a different variable. Except with var it will likely silently fail in a hard to track down way, while with let it will error in an obvious place.
No matter what you put in data, "typeof data" is never going to throw an exception.The only way to get it to throw an exception is to change sanitizeData to add a variable shadowing "data" from the parent scope.
If I use var, sanitizeData doesn't do anything and I spent a while hunting the issue down. If I use let, sanitizeData throws as soon as it's called and I can track down the issue much quicker.The only time "typeof throws an exception" is if you add a let statement later in the lexical scope in a place where if you added a var statement, it would completely change the behaviour of your code in a way you almost certainly didn't want it to.