于是就有了这个功能
Array.prototype.containsCaseInsensitive = function(obj) {
var i = this.length;
while (i--) {
if (this[i].toUpperCase() === obj.toUpperCase()) {
return true;
}
}
return false;
}
然后我创建这个数组:
ary = [0,1,2,3];
for (item in ary){
console.log(ary[item])
}
输出如下:
0
1
2
3
function(obj) {
var i = this.length;
while (i--) {
if (this[i].toUpperCase() === obj.toUpperCase()) {
return true;
}
}
return false;
}
为什么函数会在迭代中? 谢谢!
最佳答案
您的属性是可枚举的(尽管无论如何您都不应该使用 for ... in ...
枚举数组的键)。
在 ES5 兼容浏览器上,您可以使用 Object.defineProperty
安全地将该函数添加为不可枚举属性。 :
Object.defineProperty(Array.prototype, 'containsCaseInsensitive', {
value: function() {
...
}
});
关于javascript - 为什么迭代中会出现原型(prototype)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11114068/