各位好:
我在Array的Prototype属性里,自订一个each方法,如下:
Array.prototype.each = function() {
for(var i = 0; i < this.length; i++) {
console.log(this[i]);
}
}
在chrome测试以下状况:
1. 案例1:执行成功
var ary = [1,2,3];
ary.each();
======================
2. 案例2:Uncaught TypeError: Cannot read property 'each' of undefined
[1,2,3].each();
======================
3. 案例3:执行成功
var ary = [1,2,3];
ary.each();
[4,5,6].each();
======================
不懂案例2为何会执行失败,烦请解惑,谢谢。
附上执行画面:
http://imgur.com/a/Dpnzo