看板 Ajax 關於我們 聯絡資訊
各位好: 我在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://www.ptt.cc/bbs/Ajax/M.1469250321.A.0B4.html
bibo9901: 我試了是成功的喔 你應該是有別的錯誤 07/23 13:33
icydream: 執行畫面: http://imgur.com/a/Dpnzo 07/23 13:48
※ 編輯: icydream (140.118.209.53), 07/23/2016 13:51:19
jmlntw: 第10行少一個分號。 07/23 14:13
icydream: 謝謝!已順利解決 :) 07/23 14:21