[问题] 自订Array.prototype方法

楼主: icydream (巧虎)   2016-07-23 13:05:16
各位好:
我在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
作者: bibo9901 (function(){})()   2016-07-23 13:33:00
我试了是成功的喔 你应该是有别的错误
楼主: icydream (巧虎)   2016-07-23 13:48:00
作者: jmlntw (吉米林)   2016-07-23 14:13:00
第10行少一个分号。
楼主: icydream (巧虎)   2016-07-23 14:21:00
谢谢!已顺利解决 :)

Links booklink

Contact Us: admin [ a t ] ucptt.com