区别和详解:jQuery extend()和jQuery.fn.extend()

一、认识jQuery extend()和jQuery.fn.extend()javascript

jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不一样对象上方法,但在jQuery内部代码实现的是相同的,只是功能却不太同样;html

且看官方给出解释:java

jQuery.extend(): Merge the contents of two or more objects together into the first object.(把两个或者更多的对象合并到第一个当中);jquery

jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把对象挂载到jQuery的prototype属性,来扩展一个新的jQuery实例方法)ajax

二、理解jQuery.extend() ide

咱们先把jQuery当作了一个类,这样好理解一些。jQuery.extend(),是扩展的jQuery这个类。函数

假设咱们把jQuery这个类当作是人类,能吃饭能喝水能跑能跳,如今咱们用jQuery.extend这个方法给这个类拓展一个能说话speak()的技能。这样的话,不管是男人,女人,xx人.....等能继承这个技能(方法)了。spa

能够以下图这样写着:.net

JQuery.extend({
    speak:function(){
         alert("how are you!");
    }
});
调用方法以下:
<!DOCTYPE html>
<html>
<head>
    <title>jQuery.extend()与jQuery.fn.extend()区别</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery-1.7.1.js"></script>
    <script type="text/javascript">
        (function($){
               $.extend({
                   speak:function(){
                       alert("how are you!");
                   }
               });
        })(jQuery);
    </script>
    <script type="text/javascript">
        $(document).ready(function(){
            $.speak();
        })
    </script>
</head>
<body>
 
</body>
</html>

这说明$.speak)变成了jQuery这个类自己的方法(object),他如今能"说话"了。prototype

可是吧,这个能力啊,只有表明全人类的 jQuery 这个类自己,才能用啊。你我的想用,你张三李四王五麻六,你个小草民能表明全人类嘛?

因此啊,这个扩展也就是所谓的静态方法,只跟这个 类 自己有关。跟你具体的实例化对象是不要紧的。

 三、理解 jQuery.fn.extend()

从字面理解嘛,这个拓展的是jQuery.fn的方法。jQuery.fn是啥玩意呢?

jQuery.fn = jQuery.prototype = {
      init:funtion(selector,context){
            //..... 
 
     }
}

 因此jQuery.fn.extend拓展的是jQuery对象(原型的)的方法啊!

对象是啥?就是类的实例化嘛,例如$("#abc") ,$(div)

那就是说,jQuery.fn.extend拓展的方法,你得用在jQuery对象上面才行啊!他得是张三李四王五痳六这些实例化的对象才能用啊。

说白了就是得这么用(假设xyz()是拓展的方法):

$('selector').xyz();

调用方法以下:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery.extend()与jQuery.fn.extend()区别</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery-1.7.1.js"></script>
    <script type="text/javascript">
        (function($){
               $.fn.extend({
                   speak:function(){
                       alert("how are you!");
                   }
               });
        })(jQuery);
    </script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("div").speak();
        })
    </script>
</head>
<body>
 
</body>
</html>

 四、二者区别总结:

4.一、二者调用方式不一样:

       jQuery.extend(),通常由传入的全局函数来调用,主要是用来拓展个全局函数,如$.init(),$.ajax();

       jQuery.fn.extend(),通常由具体的实例对象来调用,能够用来拓展个选择器,例如$.fn.each();

4.二、二者的主要功能做用不一样:

        jQuery.extend(object); 为扩展jQuery类自己,为自身添加新的方法。

        jQuery.fn.extend(object);给jQuery对象添加方法

 4.三、大部分插件都是用jQuery.fn.extend()

 五、JQuery的extend扩展方法:

     5.一、Jquery的扩展方法原型是:

extend(dest,src1,src2,src3...);
         它的含义是将src1,src2,src3...合并到dest中,返回值为合并后的dest,由此能够看出该方法合并后,是修改了dest的结构的。
         若是想要获得合并的结果却又不想修改dest的结构,能够以下使用:
 var newSrc=$.extend({},src1,src2,src3...)//也就是将"{}"做为dest参数。

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

var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})
那么合并后的结果:  result={name:"Jerry",age:21,sex:"Boy"}

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

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

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

       就是将hello方法合并到jquery的全局对象中。

   5.2.二、$.fn.extend(src)
   该方法将src合并到jquery的实例对象中去,如:

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

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

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

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

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

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

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

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

 extend(boolean,dest,src1,src2,src3...)

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

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,那么合并后的结果就是: 

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

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

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

        那么合并后的结果就是:

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

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

相关文章
相关标签/搜索