【发布时间】:2011-10-11 07:35:29
【问题描述】:
Array.prototype.testi = function() {console.log('hep');}
var b = new Array();
b.push(1, 5, 'b', 'c');
for (var i in b) {
console.log(b[i]);
}
这将登录(在 chrome 中)
1
2
b
c
function () {console.log('hep');}
或http://jsfiddle.net/marqs/6VVka/1/中的实时示例
我的问题是,为什么列表中显示的函数,即使它被赋予了数组原型?
这对扩展未使用的浏览器功能有影响(例如,如果使用 array in 等,IE 扩展的 Array.filter 的行为会有所不同)。
编辑: 澄清一下,我无法访问执行 for-in 循环的代码,因为它是一个外部库。因此,问题的提出,更多的是关于“为什么会这样”而不是如何解决这个问题。
【问题讨论】:
标签: javascript definition function-prototypes