`
lxs647
  • 浏览: 516780 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jQuery.extend 函数详解【转】

阅读更多

jQuery.extend 函数详解

 

JQuery的extend扩展方法:
      Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。
      一、Jquery的扩展方法原型是:   

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> extend(dest,src1,src2,src3...);


      它的含义是将src1,src2,src3...合并到dest中,返回值为合并后的dest,由此可以看出该方法合并后,是修改了dest的结构的。如果想要得到合并的结果却又不想修改dest的结构,可以如下使用:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> var newSrc=$.extend({},src1,src2,src3...)//也就是将"{}"作为dest参数。


      这样就可以将src1,src2,src3...进行合并,然后将合并结果返回给newSrc了。如下例:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})

 

      那么合并后的结果

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> result={name:"Jerry",age:21,sex:"Boy"}


      也就是说后面的参数如果和前面的参数存在相同的名称,那么后面的会覆盖前面的参数值。

      二、省略dest参数
      上述的extend方法原型中的dest参数是可以省略的,如果省略了,则该方法就只能有一个src参数,而且是将该src合并到调用extend方法的对象中去,如:
   1、$.extend(src)
   该方法就是将src合并到jquery的全局对象中去,如:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $.extend({ hello:function(){alert('hello');} });


   就是将hello方法合并到jquery的全局对象中。
   2、$.fn.extend(src)
   该方法将src合并到jquery的实例对象中去,如:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $.fn.extend({ hello:function(){alert('hello');} });

 

   就是将hello方法合并到jquery的实例对象中。

   下面例举几个常用的扩展实例:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->$.extend({net:{}});

 

   这是在jquery全局对象中扩展一个net命名空间。

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $.extend($.net,{ hello:function(){alert('hello');} })


    这是将hello方法扩展到之前扩展的Jquery的net命名空间中去。

   三、Jquery的extend方法还有一个重载原型:  

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->extend(boolean,dest,src1,src2,src3...)


      第一个参数boolean代表是否进行深度拷贝,其余参数和前面介绍的一致,什么叫深层拷贝,我们看一个例子:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->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"},第一个深度拷贝参数为true,那么合并后的结果就是: 

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->result={name:"John",last:"Resig", location:{city:"Boston",state:"MA",county:"China"}}

 

       也就是说它会将src中的嵌套子对象也进行合并,而如果第一个参数boolean为false,我们看看合并的结果是什么,如下:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->var result=$.extend( false, {}, { name: "John", location:{city: "Boston",county:"USA"} }, { last: "Resig", location: {state: "MA",county:"China"} } );


     那么合并后的结果就是:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> result={name:"John",last:"Resig",location:{state:"MA",county:"China"}}

 

  以上就是$.extend()在项目中经常会使用到的一些细节。

分享到:
评论

相关推荐

    jQuery.extend 函数详解

    jQuery.extend 函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。

    jQuery:jQuery.extend函数详解

    jQuery:jQuery.extend函数详解

    jQuery.extend()、jQuery.fn.extend()扩展方法示例详解

    其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方法能够创建jQuery对象方法. 例如: 代码如下: jQuery.extend({ showName : function(name){ alert&#40;name&#41; } }); jQuery.showName...

    jQuery_extend 函数详解

    jQuery_extend 函数详解

    jQuery.extend 函数及用法详细

    jquery.extend函数详解 JQuery的extend扩展方法:  Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。 一、Jquery的扩展方法原型是: extend(dest,src1...

    jQuery中extend函数的实现原理详解

    extend()是jQuery中一个重要的函数,作用是实现对对象的扩展, 它经常用于jQuery插件的开发,jQuery内部也使用它来扩展属性方法,如上篇文章中讲到的noConflict方法,就是用extend方法来扩展的。 在jQuery的API手册...

    jQuery中extend()和fn.extend()方法详解

    jQuery自定义了jQuery.extend()和jQuery.fn.extend()方法.其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方法能够创建jQuery对象方法.

    jQuery中extend函数详解

    1.$.extend({},defaults, options) 这样做的目的是为了保护包默认参数。也就是defaults里面的参数。 做法是将一个新的空对象({})做为$.extend的第一个参数,defaults和用户传递的参数对象紧随其后,这样做的好处是...

    jQuery.extend()的实现方式详解及实例

    extend()函数是jQuery的基础函数之一,作用是扩展现有的对象

    jQuery 自定义函数写法分享

    jQuery.extend 函数详解JQuery的extend扩展方法:Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。一、Jquery的扩展方法原型是:   

    详解jQuery插件开发中的extend方法

    在jQuery中定义 jQuery.extend = jQuery.fn.extend 所以这两个函数式相同的  一、Jquery的扩展方法原型是:  extend(dest,src1,src2,src3…); 它的含义是将src1,src2,src3…合并到dest中,返回值为合并

    jQuery继承extend用法详解

    主要为大家详细介绍了jQuery继承extend用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    jQuery弹框插件使用方法详解

    3、插件的要素(扩展jQuery本身的方法,$.extend ; 给jQuery对象添加方法,对jQuery.prototype进行扩展 ;添加一个函数到jQuery.fn(jQuery.prototype)对象,该函数的名称就是你的插件名称) 4、代码部分: 注意...

    跟我一起学JQuery插件开发

    【3】RascallySnake的JQuery.extend()详解一.介绍 插件编写的目的是给已经有的一系列方法或函数做一个封装,以便在其他地方重复使用,方便后期维护。 JQuery除了提供一个简单、有效的方式进行管理元素以及脚

Global site tag (gtag.js) - Google Analytics