jQuery是否以任何顺序接受参数?

时间:2013-01-31 09:32:05

标签: javascript parameters conditional-statements jquery

我注意到在使用jQuery.fn.animate()时,我可以按任意顺序将部分或全部参数(css for animation,callback function,easing和duration)传递给函数。

查看函数源代码,它的开头如下:

function (prop, speed, easing, callback) {
    var empty = jQuery.isEmptyObject(prop),
        optall = jQuery.speed(speed, easing, callback),
.
.
.

然后它开始实际处理传递的信息。因此,显然css属性对象必须首先在链中 - 否则它将破坏函数(或者是它?)。但是看看jQuery.speed:

function (speed, easing, fn) {
    var opt = speed && typeof speed === "object" ? jQuery.extend({},
    speed) : {
        complete: fn || !fn && easing || jQuery.isFunction(speed) && speed,
        duration: speed,
        easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
    };
.
.
.

显然这就是神奇的地方。但是jQuery减少括号和括号的方法使我很难分解所有这些条件。你能简化一下jQuery.speed函数吗?感谢。

1 个答案:

答案 0 :(得分:1)

以更全面的方式编写,这就是jQuery的作用:

function (speed, easing, fn) {
    var opt;
    if (speed && typeof speed === "object") {
        opt = jQuery.extend({}, speed);
    }
    else {
        var complete_p,
            duration_p = speed,
            easing_p;

        // Find out what's the "complete" property
        if (fn) complete_p = fn;
        else {
            if (easing) {
                complete_p = easing;
            }
            else {
                if (jQuery.isFunction(speed)) {
                    complete_p = speed;
                }
            }
        }

        // Find out what's the "easing" property
        if (fn) {
            easing_p = easing;
        }
        else {
            if (easing) {
                if (!jQuery.isFunction(easing)) {
                    easing_p = easing;
                }
            }
        }

        opt = {
            complete: complete_p,
            duration: duration_p,
            easing: easing_p
        };
    }
.
.
.

所以......是的,它正在进行一些检查以允许您更改订单。不过,我不会依赖它。