作者icydream (巧虎)
看板Ajax
标题[问题] 自订Array.prototype方法
时间Sat Jul 23 13:05:16 2016
各位好:
我在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
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.118.209.53
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1469250321.A.0B4.html
1F:→ bibo9901: 我试了是成功的喔 你应该是有别的错误 07/23 13:33
※ 编辑: icydream (140.118.209.53), 07/23/2016 13:51:19
3F:推 jmlntw: 第10行少一个分号。 07/23 14:13
4F:→ icydream: 谢谢!已顺利解决 :) 07/23 14:21