在javascript/jquery中传递可选参数



JQuery .load()的定义如下:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );
datacomplete()参数是可选的。对吧?但是,我们怎么调用这个呢:
$('#result').load('ajax/test.html', function(){
    alert('Load was performed.');
}

jquery如何忽略第二个参数并知道我们提供了第一个和3d参数?如果3d参数不是一个函数呢?

查看源代码

load: function( url, params, callback ) {
    if (typeof url !== "string" && _load) {
        // My comment: the function.apply ia an 
        // alternative way to call a javascript function, 
        // where the number of parameter in `arguments` 
        // couldn't be determined
        // the `arguments` is a special javascript variable. 
        // It's an array containing the parameters within 
        // the context of a function
        return _load.apply(this, arguments);
    // Don't do a request if no elements are being requested        
    } else if ( !this.length ) {            
        return this;        
    }       
    var off = url.indexOf( " " );       
    if ( off >= 0 ) {           
        var selector = url.slice( off, url.length );            
        url = url.slice( 0, off );      
    }       
    // Default to a GET request     
    var type = "GET";       
    // If the second parameter was provided     
    if ( params ) {         
        // If it's a function           
        if ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback             
            callback = params;              
            params = undefined;         
            // Otherwise, build a param string          
        } else if ( typeof params === "object" ) {              
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );               
            type = "POST";          
        }       
    }
 .... other code

如果使用两个或更多参数调用load, jQuery将检查第二个参数是函数还是对象。如果它是一个函数,则在ajax调用完成时调用它。否则,它被用作ajax调用传递的参数。

来源相关部分:

if ( params ) {
    // If it's a function
        if ( jQuery.isFunction( params ) ) {
        // We assume that it's the callback
        callback = params;
        params = null;
    // Otherwise, build a param string
    } else if ( typeof params === "object" ) {
        params = jQuery.param( params, jQuery.ajaxSettings.traditional );
        type = "POST";
    }
}

其中paramsload的第二个参数。

isFunction是以下函数的结果:

isFunction: function( obj ) {
    return jQuery.type(obj) === "function";
},
type: function( obj ) {
    return obj == null ?
        String( obj ) :
        class2type[ toString.call(obj) ] || "object";
},

其中class2type是一个关联数组,其中包含以下元素:

class2type[ "[object Function]" ] = "function";

让我们看看源代码,因此您将看到第二个参数是否是一个函数,然后将第二个参数params传递给第三个参数callback,并将第二个参数params设置为undefined

    // If the second parameter was provided
    if ( params ) {
        // If it's a function
        if ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback
            callback = params;
            params = undefined;
        // Otherwise, build a param string
        } else if ( typeof params === "object" ) {
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );
            type = "POST";
        }
    }

相关内容

  • 没有找到相关文章

最新更新