首页 > 请会用js的哥们帮分析一下以下代码

请会用js的哥们帮分析一下以下代码

尤其是while 那里,整个方法是类似于jquery .each()的一种方法。其实我不大理解他这代码为啥不用each.

//Forward fast each
$.fn
    .fwdEach = (function () {
    var jq = jQuery([1]);
    return function (c) {
        var i = -1,
            el, len = this.length;
        try {
            while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
        } catch (e) {
            delete jq[0];
            throw e;
        }
        delete jq[0];
        return this;
    };
}());

fwdEach会准备好对应element的jQuery object,作为this传递给回调函数。
因为jQuery object jq是重复利用的,性能会略好于在回调函数内调用var jq = $(this);

【热门文章】
【热门文章】