;(function(undefined){
// 針對 iOS 3.2 及以下版本的 ipad, iphone 自帶瀏覽器String對象不帶trim做的瀏覽器polyfill
if (String.prototype.trim === undefined) // fix for iOS 3.2
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g, '') }
// 針對 iOS 3.X 及以下版本的 ipad, iphone 自帶瀏覽器Array對象不帶reduce做的瀏覽器polyfill,不過貌似忽視了reduceRight
if (Array.prototype.reduce === undefined)
Array.prototype.reduce = function(fun){
// 除去一些不能操作的特殊情況,這種時候要拋出類型異常
if(this === void 0 || this === null) throw new TypeError()
// 將傳入數組轉化Object對象
var t = Object(this), len = t.length >>> 0, k = 0, accumulator
// 如果傳入的參數不是一個回調函數,那麼不能進行處理,拋出類型異常
if(typeof fun != 'function') throw new TypeError()
// 如果傳入的不是一個數組或者類數組,且只傳入了回調,那麼沒有對象操作,不能處理,拋出類型異常
if(len == 0 && arguments.length == 1) throw new TypeError()
// 給 accumulator 賦初值,即傳入的數組對象
if(arguments.length >= 2)
accumulator = arguments[1]
else
do{
if(k in t){
accumulator = t[k++]
break
}
if(++k >= len) throw new TypeError()
} while (true)
// 執行傳入的回調
while (k < len){
if(k in t) accumulator = fun.call(undefined, accumulator, t[k], k, t)
k++
}
return accumulator
}
})()
這一個模塊主要是針對 IOS3 做的瀏覽器 polyfill 。爲瀏覽器添加 String.prototype.trim 與 Array.prototype.reduce 兩個方法