Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,7 +248,7 @@ if (!Array.prototype.some) { | |
if (!Array.prototype.reduce) { | ||
Array.prototype.reduce = function reduce(fun /*, initial*/) { | ||
var len = +this.length; | ||
if (typeof fun !== "function") | ||
if (typeof fun !== "function" || fun instanceof RegExp) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kriskowal
Author
Member
|
||
throw new TypeError(); | ||
|
||
// no value to return if no initial value and an empty array | ||
|
@@ -280,6 +280,7 @@ if (!Array.prototype.reduce) { | |
}; | ||
} | ||
|
||
|
||
// ES5 15.4.4.22 | ||
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight | ||
if (!Array.prototype.reduceRight) { | ||
|
@@ -329,7 +330,7 @@ if (!Array.prototype.indexOf) { | |
if (i < 0) | ||
i += length; | ||
for (; i < length; i++) { | ||
if (!owns(this, i)) | ||
if (!(i in this)) | ||
continue; | ||
if (value === this[i]) | ||
return i; | ||
|
@@ -349,7 +350,7 @@ if (!Array.prototype.lastIndexOf) { | |
i += length; | ||
i = Math.min(i, length - 1); | ||
for (; i >= 0; i--) { | ||
if (!owns(this, i)) | ||
if (!(i in this)) | ||
continue; | ||
if (value === this[i]) | ||
return i; | ||
|
There's no need for a
RegExp
check here. Iftypeof X
produces"function"
thenX
implements [[Call]].edit: I know you might say "But, Michael, the spec says 'callbackfn should be a function that takes four arguments.'". And you'd be right. But what is a "function" in that sentence? That's not a technical definition. The only requirement for
callbackfn
in the technical section is that it implement [[Call]].edit 2: You might also say "But, Michael, my browser throws a
TypeError
when I pass aRegExp
instance toArray.prototype.reduce
". That's because your browser likely didn't fully implement callableRegExp
s as they are just a short-term experiment.edit 3: If you absolutely must disallow all [[Call]]able objects that are not
Function
instances, make a whitelist, not a blacklist.Object.prototype.toString.call(fun) != "[object Function]"
should do the trick.