简体   繁体   English

在javascript / jquery中传递可选参数

[英]passing optional parameters in javascript/jquery

The JQuery .load() definition follows: JQuery .load()定义如下:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );

The data and complete() arguments are optional. datacomplete()参数是可选的。 Right? 对? But then, how we can call this: 但是,然后我们可以这样称呼它:

$('#result').load('ajax/test.html', function(){
    alert('Load was performed.');
}

How jquery ignores the 2nd argument and knows that we provided the 1st and the 3d argument? jQuery如何忽略第二个参数并知道我们提供了第一个和第3d个参数? And what if the 3d argument is not a function? 如果3d参数不是函数呢?

See the source code . 参见源代码

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

If load is called with two arguments or more, jQuery checks to see if the second argument is a function or an object. 如果使用两个或多个参数调用了load ,则jQuery会检查第二个参数是函数还是对象。 If it's a function it's called when the ajax call is completed. 如果它是一个函数,则在ajax调用完成时会调用它。 Otherwise it's used as the params passed with the ajax call. 否则,它将用作通过ajax调用传递的参数。

Relevant parts from the source: 来源中的相关零件:

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";
    }
}

Where params is the second argument to load . 其中params是要load的第二个参数。

isFunction is the result of the following functions: isFunction是以下功能的结果:

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

where class2type is an assosiative array containing, among other things, this element: 其中class2type是一个class2type数组,其中包含以下元素:

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

Let's see the source code, so you will see if the second parameter is a function, then pass the second parameter params to the third parameter callback and set the second parameter params to undefined . 让我们看一下源代码,因此您将看到第二个参数是否是一个函数,然后将第二个参数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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM