Wednesday, November 17, 2010

jQuery extend() removes undefined values...

If you pass in an undefined value into the data for the $.ajax call(), this undefined field will be removed during the $.ajax() call when extending the variables:

var params = { width:1680, height:1050, picture: undefined };

$.ajax({url: '', data: params});

This will invoke the $.ajax function, which then invokes the extend() function:
   ajax: function( origSettings ) {
        var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);

The extend() function tries to remove out the undefined/null values:
jQuery.extend = jQuery.fn.extend = function() {
       // Only deal with non-null/undefined values                                                                                                                                                                                          
        if ( (options = arguments[ i ]) != null ) {
            // Extend the base object                                                                                                                                                                                                        
                // Don't bring in undefined values                                                                                                                                                                                           
                } else if ( copy !== undefined ) {
                    target[ name ] = copy;

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training in Chennai . or learn thru JQuery Training . or learn thru ES6 Online Training. Nowadays JavaScript has tons of job opportunities on various vertical industry.