【发布时间】:2018-09-26 05:09:34
【问题描述】:
Array.prototype._find = function(callbackfn, thisArg) {
if (this == null) {
throw new TypeError('this is null or not defined')
}
if (typeof callbackfn !== 'function') {
throw new TypeError('callbackfn is not a function')
}
for (let i in this) {
if (callbackfn.call(thisArg, this[i], i, this)) return this[i]
}
return undefined
}
console.log(Array.prototype._find.call(null, x => x > 21))
这是一个Array.prototype.find polyfill,除了运行console.log(Array.prototype._find.call(null, x => x > 21))时触发TypeError,我很困惑为什么不触发TypeError
【问题讨论】:
标签: javascript arrays find polyfills