在向Array原型添加方法后,某些不相关的脚本出现了问题。
- [Opera] 未处理错误:'this.reduce'不是一个函数
- [Firefox] TypeError:this.reduce不是一个函数
该方法本身有效([1,2,3].xintsum()
正常输出 6
)。
// adding a function to the Array prototype
Array.prototype.xintsum = function() { return this.reduce(function(old, add) {return old + add;}, 0); };
// accessing the array in a way that worked before
$(document).ready(function (){
var some_array = [];
for (head_n in some_array) {
var v = some_array[head_n];
$('<th></th>').text(v);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
this
应该引用什么?你确定它是一个array
吗? - Sterling Archer