Defer to the built-in typeof if support for symbols exists.#3218
Defer to the built-in typeof if support for symbols exists.#3218hzoo merged 1 commit intobabel:masterfrom jdalton:_typeof
typeof if support for symbols exists.#3218Conversation
Current coverage is
|
There was a problem hiding this comment.
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.
@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
typeofoperator if support for symbols exists.