欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery中extend的用法實例分析

 更新時間:2015年02月08日 10:30:13   作者:whazhl  
這篇文章主要介紹了JQuery中extend的用法,實例分析了extend的功能、定義及相關(guān)使用技巧,需要的朋友可以參考下

本文實例講述了JQuery中extend的用法。分享給大家供大家參考。具體分析如下:

extend()函數(shù)是jQuery的基礎(chǔ)函數(shù)之一,作用是擴(kuò)展現(xiàn)有的對象。extend是我們在寫插件的過程中常用的方法,該方法有一些重載原型。$.extend(prop) 用于擴(kuò)展jQuery對象,可以用于把函數(shù)添加到j(luò)Query名稱空間中。

一、jQuery.extend函數(shù)的源碼

jQuery.extend = jQuery.fn.extend = function() {
  var options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},//參數(shù)目標(biāo)對象
    i = 1,
    length = arguments.length,//參數(shù)長度
    deep = false;//是否為深度復(fù)制

  // Handle a deep copy situation
  //如果為深度復(fù)制,則目標(biāo)對象和原對象游標(biāo)值i,以及深度值都進(jìn)行更新
  if ( typeof target === "boolean" ) {
    deep = target;
    target = arguments[1] || {};
    // skip the boolean and the target
    i = 2;
  }

  // Handle case when target is a string or something (possible in deep copy)
  //當(dāng)目標(biāo)對象的值類型錯誤,則重置為{}
  if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
    target = {};
  }

  // extend jQuery itself if only one argument is passed
  //當(dāng)參數(shù)值長度為1的情況下,目標(biāo)對象就為jQuery自身
  if ( length === i ) {
    target = this;
    --i;
  }

  for ( ; i < length; i++ ) {
    // Only deal with non-null/undefined values
    if ( (options = arguments[ i ]) != null ) {//忽略空對象
      // Extend the base object
      for ( name in options ) {
        src = target[ name ];
        copy = options[ name ];//存儲對象的值

        // Prevent never-ending loop
        if ( target === copy ) {
          continue;
        }

        // Recurse if we're merging plain objects or arrays
         //深度復(fù)制只有屬性深度多于倆層的對象關(guān)系的結(jié)構(gòu)的,如{a:{s:21,age:11}}或{a:['s'=>21,'age'=>11]}
        if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
          if ( copyIsArray ) {//如果是數(shù)組對象
            copyIsArray = false;
            clone = src && jQuery.isArray(src) ? src : [];

          } else {//普通對象
            clone = src && jQuery.isPlainObject(src) ? src : {};
          }

          // Never move original objects, clone them
          // 調(diào)用自身進(jìn)行遞歸復(fù)制
          target[ name ] = jQuery.extend( deep, clone, copy );

        // Don't bring in undefined values
        } else if ( copy !== undefined ) {//非深度CP直接覆蓋目標(biāo)屬性
          target[ name ] = copy;
        }
      }
    }
  }

  // Return the modified object
  return target;
};

二、Jquery的擴(kuò)展方法原型是:
  
1、extend(dest,src1,src2,src3...);

它的含義是將src1,src2,src3...合并到dest中,返回值為合并后的dest,由此可以看出該方法合并后,是修改了dest的結(jié)構(gòu)的。如果想要得到合并的結(jié)果卻又不想修改dest的結(jié)構(gòu),可以如下使用:
 
2、var newSrc=$.extend({},src1,src2,src3...)//也就是將"{}"作為dest參數(shù)。

這樣就可以將src1,src2,src3...進(jìn)行合并,然后將合并結(jié)果返回給newSrc了。

如下例:

var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})

那么合并后的結(jié)果

result={name:"Jerry",age:21,sex:"Boy"}

也就是說后面的參數(shù)如果和前面的參數(shù)存在相同的名稱,那么后面的會覆蓋前面的參數(shù)值。
 
3、extend(boolean,dest,src1,src2,src3...)

第一個參數(shù)boolean代表是否進(jìn)行深度拷貝,其余參數(shù)和前面介紹的一致
例如:

var result=$.extend( true, {}, 
{ name: "John", location: {city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } );

我們可以看出src1中嵌套子對象location:{city:"Boston"},src2中也嵌套子對象location:{state:"MA"},第一個深度拷貝參數(shù)為true,那么合并后的結(jié)果就是:

result={name:"John",last:"Resig",location:{city:"Boston",state:"MA",county:"China"}}

也就是說它會將src中的嵌套子對象也進(jìn)行合并,而如果第一個參數(shù)boolean為false,我們看看合并的結(jié)果是什么,如下:

var result=$.extend( false, {}, 
{ name: "John", location:{city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } );

那么合并后的結(jié)果就是:

result={name:"John",last:"Resig",location:{state:"MA",county:"China"}}

二、Jquery中extend方法省略dest參數(shù)的情況
 
上述的extend方法原型中的dest參數(shù)是可以省略的,如果省略了,則該方法就只能有一個src參數(shù),而且是將該src合并到調(diào)用extend方法的對象中去,如:

1、$.extend(src)

該方法就是將src合并到j(luò)query的全局對象中去,如:

$.extend({
 hello:function(){alert('hello');}
});

就是將hello方法合并到j(luò)query的全局對象中。
 
2、$.fn.extend(src)

該方法將src合并到j(luò)query的實例對象中去,如:

$.fn.extend({
 hello:function(){alert('hello');}
});

就是將hello方法合并到j(luò)query的實例對象中。
 
三、下面例舉幾個常用的擴(kuò)展實例:

$.extend({net:{}});

這是在jquery全局對象中擴(kuò)展一個net命名空間。

$.extend($.net,{
  hello:function(){alert('hello');}
})

這是將hello方法擴(kuò)展到之前擴(kuò)展的Jquery的net命名空間中去。

希望本文所述對大家的jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論