;(function(){
try {
getComputedStyle(undefined)
} catch(e) {
var nativeGetComputedStyle = getComputedStyle
window.getComputedStyle = function(element, pseudoElement){
try {
return nativeGetComputedStyle(element, pseudoElement)
} catch(e) {
return null
}
}
}
})()
這段代碼的主要作用是改寫 getComputedStyle 方法。主要是爲了 zepto 在不同的瀏覽器下,可以進行相同的操作。通過查看代碼,我們可以看出主要使用的方法是利用 try…catch… 來進行異常處理。同時這是一個立即執行函數,使得我們在加載框架的時候,就得以重寫 getComputedStyle 方法。
這段代碼主要是針對在不同參數數量情況下,不同瀏覽器對此方法產生的處理差異性的統一。