-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Defer to the built-in typeof
if support for symbols exists.
#3218
Conversation
Current coverage is
|
(function (obj) { | ||
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
}); | ||
(typeof Symbol === "function" && typeof Symbol() === "symbol") |
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.
Forgive me for lacking context here.
Does this mean every use of typeof will call Symbol() or does this result get cached and the resulting function is used?
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.
@gnarf
I'm not a pro at Babel's helpers, I based this PR off of the extends helper, but the idea is that this is an expression that will be assigned to an identifier. So the end result would look something like
var _typeof = (typeof Symbol === "function" && typeof Symbol() === "symbol")
? function (obj) { return typeof obj; }
: function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
So modern engines would get a _typeof
helper that's only
function (obj) { return typeof obj; }
I can explain the problem. Without external helpers / Ok, how we can solve this problem? We can add mark to the (typeof Symbol === "function" && !Symbol._shim) ? foo : bar; Or we can cache the result of this detection: (typeof Symbol === "function" && !('_shim' in Symbol ? Symbol._shim : (Symbol._shim = typeof Symbol() != "symbol"))) ? foo : bar; |
Changed the check to (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") |
It makes sense. LGTM. |
Every engine with native Symbols implements |
Defer to the built-in `typeof` if support for symbols exists.
Looks good - thanks! |
This PR addresses https://phabricator.babeljs.io/T6875 by deferring to the built-in
typeof
operator if support for symbols exists.