开发者

SO: what does this js mean? I am studying SO source code [closed]

开发者 https://www.devze.com 2023-01-21 16:19 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

(some code)(jQuery);

What does this mean?

(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);


It is a closure.

It is a part of your code that can have own variables that will not be shared with the rest of your code.

And the final parenthesis have the parameters passed to this closure.

Like this:

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

In your example the closure is being calling with jquery object as parameter.

With this you can isolate the execution of this part of your code. It is powerful and must be well know to programming real javascript.

To view more information about closures in javascript see this: http://jibbering.com/faq/notes/closures/

0

精彩评论

暂无评论...
验证码 换一张
取 消