2010-10-25 37 views
0

(部分代碼)(jQuery);SO:這是什麼意思?我正在學習SO源代碼

這是什麼意思?

(function (a) { 
    function d(g) { 
     return typeof g == "object" ? g : { 
      top: g, 
      left: g 
     } 
    } 
    var b = a.scrollTo = function (g, e, f) { 
     a(window).scrollTo(g, e, f) 
    }; 
    b.defaults = { 
     axis: "xy", 
     duration: parseFloat(a.fn.jquery) >= 1.3 ? 0 : 1 
    }; 
    b.window = function() { 
     return a(window)._scrollable() 
    }; 
    a.fn._scrollable = function() { 
     return this.map(function() { 
      if (!(!this.nodeName || a.inArray(this.nodeName.toLowerCase(), ["iframe", "#document", "html", "body"]) != -1)) return this; 
      var g = (this.contentWindow || this).document || this.ownerDocument || this; 
      return a.browser.safari || g.compatMode == "BackCompat" ? g.body : g.documentElement 
     }) 
    }; 
    a.fn.scrollTo = function (g, e, f) { 
     if (typeof e == "object") { 
      f = e; 
      e = 0 
     } 
     if (typeof f == "function") f = { 
      onAfter: f 
     }; 
     if (g == "max") g = 9E9; 
     f = a.extend({}, b.defaults, f); 
     e = e || f.speed || f.duration; 
     f.queue = f.queue && f.axis.length > 1; 
     if (f.queue) e /= 2; 
     f.offset = d(f.offset); 
     f.over = d(f.over); 
     return this._scrollable().each(function() { 
      function k(w) { 
       j.animate(r, e, f.easing, w && 
       function() { 
        w.call(this, g, f) 
       }) 
      } 
      var h = this, 
       j = a(h), 
       i = g, 
       m, r = {}, 
       u = j.is("html,body"); 
      switch (typeof i) { 
      case "number": 
      case "string": 
       if (/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(i)) { 
        i = 
        d(i); 
        break 
       } 
       i = a(i, this); 
      case "object": 
       if (i.is || i.style) m = (i = a(i)).offset() 
      } 
      a.each(f.axis.split(""), function (w, v) { 
       var q = v == "x" ? "Left" : "Top", 
        s = q.toLowerCase(), 
        y = "scroll" + q, 
        D = h[y], 
        H = b.max(h, v); 
       if (m) { 
        r[y] = m[s] + (u ? 0 : D - j.offset()[s]); 
        if (f.margin) { 
         r[y] -= parseInt(i.css("margin" + q)) || 0; 
         r[y] -= parseInt(i.css("border" + q + "Width")) || 0 
        } 
        r[y] += f.offset[s] || 0; 
        if (f.over[s]) r[y] += i[v == "x" ? "width" : "height"]() * f.over[s] 
       } else { 
        q = i[s]; 
        r[y] = q.slice && q.slice(-1) == "%" ? parseFloat(q)/100 * H : q 
       } 
       if (/^\d+$/.test(r[y])) r[y] = r[y] <= 0 ? 0 : Math.min(r[y], H); 
       if (!w && f.queue) { 
        D != r[y] && k(f.onAfterFirst); 
        delete r[y] 
       } 
      }); 
      k(f.onAfter) 
     }).end() 
    }; 
    b.max = function (g, e) { 
     var f = e == "x" ? "Width" : "Height", 
      k = "scroll" + f; 
     if (!a(g).is("html,body")) return g[k] - a(g)[f.toLowerCase()](); 
     f = "client" + f; 
     var h = g.ownerDocument.documentElement, 
      j = g.ownerDocument.body; 
     return Math.max(h[k], j[k]) - Math.min(h[f], j[f]) 
    } 
})(jQuery); 
+1

在什麼情況下是這樣的代碼?你在哪裏找到它? – Marm0t 2010-10-25 12:46:40

+4

加油!你真的期望有人向你一行一行解釋嗎?請提出更具體的問題。 – 2010-10-25 12:46:53

+0

我的意思是(...)(jQuery)的目的是什麼?傳遞jQuery作爲這個函數的參數?擴展jQuery? – 2010-10-25 12:51:17

回答

2

這是一個閉包。

這是你的代碼的一部分,可以有自己的變量,不會與其他代碼共享。

最後的括號將參數傳遞給此閉包。

像這樣:

(function (what, howmany){ 
    for (var i = howmany; i--;){ 
     alert(what); 
    } 
})("John",3); 

在你的榜樣關閉正在使用jQuery對象作爲參數調用。

有了這個,你可以隔離這部分代碼的執行。它是強大的,必須很好地編程真正的JavaScript。

要查看有關關閉的詳細信息在JavaScript中看到這一點:http://jibbering.com/faq/notes/closures/

+0

太棒了!謝謝!! – 2010-10-25 13:10:33