-
-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add historical note to no-typeof-undefined
#1992
Conversation
I don't know what that mean. I don't think use |
Before strict mode, |
In strict mode |
I believe the global It has nothing to do with the strict mode. $ node -p "(function() {'use strict';const undefined = 1; return typeof undefined})()"
number
$ node -p "(function() {const undefined = 1; return typeof undefined})()"
number |
Gotcha, so the rule was related to pre-ES5 browsers. I'll update the wording |
@@ -9,6 +9,8 @@ | |||
|
|||
Checking if a value is `undefined` by using `typeof value === 'undefined'` is needlessly verbose. It's generally better to compare against `undefined` directly. The only time `typeof` is needed is when a global variable potentially does not exists, in which case, using `globalThis.value === undefined` may be better. | |||
|
|||
Historical note: Comparing against `undefined` without `typeof` was frowned upon until ES5. This is no longer a problem since all engines currently in use no longer allow reassigning the `undefined` global. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this more correct? It seems that IE9 did this correctly already according to http://kangax.github.io/compat-table/es5/ (IE8 tests missing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this, I use typeof
(since long time ago, maybe before IE6) not because I'm worried about undefined
reassigned, but worried about error thrown on undefined global variable. But I guess it's fine to add this to the doc. Won't block.
This rule seemingly goes against the "best practice" of always using
typeof
. I'd like to explain that the practice is no longer "best", but I'm not sure if this is the best way to phrase it.