Created
December 6, 2011 12:58
-
-
Save unscriptable/1438121 to your computer and use it in GitHub Desktop.
Closure compiler is too smart for ES5 shims
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// google closure compiler turns this: | |
function indexOf (item /*, fromIndex */) { | |
return find(this, item, arguments[1] /* fromIndex */, true); | |
} | |
// into this (notice the new function argument): | |
function q(a,c){b(this,a,c,!0)} | |
/* | |
Q: Why is this bad? | |
A: Because ES5 (http://es5.github.com/#x15.4.4.14) says that | |
the function signature for Array.prototype.indexOf must | |
have length == 1. For strict ES5 compliance, there cannot | |
be extra arguments. | |
*/ | |
// How to fix it: | |
function indexOf (item /*, fromIndex */) { | |
// Notice the +1 below. Closure compiler thinks it's an equation? | |
return find(this, item, arguments[+1] /* fromIndex */, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep. That's the code! I see there are no configuration options to alter its behavior either.
The intent of the spec is not to throw exceptions or otherwise limit what arguments devs can provide to indexOf. The fromIndex parameter is still allowed in strict mode. (I checked with the code below.) Instead, I think it's to provide more meaningful introspection. That's just a guess since the spec doesn't say.