您好,欢迎访问一九零五行业门户网

详解jQuery中extend()和jQuery.fn.extend()的区别

相关推荐:《jquery视频教程》
1、认识jquery extend()和jquery.fn.extend()
jquery的api手册中,extend方法挂载在jquery和jquery.fn两个不同对象上方法,但在jquery内部代码实现的是相同的,只是功能却不太一样;
且看官方给出解释:
jquery.extend(): merge the contents of two or more objects together into the first object.(把两个或者更多的对象合并到第一个当中);
jquery.fn.extend():merge the contents of an object onto the jquery prototype to provide new jquery instance methods.(把对象挂载到jquery的prototype属性,来扩展一个新的jquery实例方法)
2、理解jquery.extend() 
我们先把jquery看成了一个类,这样好理解一些。jquery.extend(),是扩展的jquery这个类。
假设我们把jquery这个类看成是人类,能吃饭能喝水能跑能跳,现在我们用jquery.extend这个方法给这个类拓展一个能说话speak()的技能。这样的话,不论是男人,女人,xx人.....等能继承这个技能(方法)了。
可以如下图这样写着:
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),他现在能说话了。
但是吧,这个能力啊,只有代表全人类的 jquery 这个类本身,才能用啊。你个人想用,你张三李四王五麻六,你个小草民能代表全人类嘛?
所以啊,这个扩展也就是所谓的静态方法,只跟这个 类 本身有关。跟你具体的实例化对象是没关系的。
 3、理解 jquery.fn.extend()
从字面理解嘛,这个拓展的是jquery.fn的方法。jquery.fn是啥玩意呢?
jquery.fn = jquery.prototype = {      init:funtion(selector,context){            //.....       }}
所以jquery.fn.extend拓展的是jquery对象(原型的)的方法啊!
对象是啥?就是类的实例化嘛,例如$(#abc) ,$(p)
那就是说,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(){            $(p).speak();        })    </script></head><body> </body></html>
4、两者区别总结:
4.1、两者调用方式不同:
       jquery.extend(),一般由传入的全局函数来调用,主要是用来拓展个全局函数,如$.init(),$.ajax();
       jquery.fn.extend(),一般由具体的实例对象来调用,可以用来拓展个选择器,例如$.fn.each();
4.2、两者的主要功能作用不同:
        jquery.extend(object); 为扩展jquery类本身,为自身添加新的方法。
        jquery.fn.extend(object);给jquery对象添加方法
 4.3、大部分插件都是用jquery.fn.extend()
 5、jquery的extend扩展方法:
     5.1、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.2、省略dest参数
           上述的extend方法原型中的dest参数是可以省略的,如果省略了,则该方法就只能有一个src参数,而且是将该src合并到调用extend方法的对象中去,如:
 5.2.1、$.extend(src)
 该方法就是将src合并到jquery的全局对象中去,如:
  $.extend({      hello:function(){alert('hello');}  });
就是将hello方法合并到jquery的全局对象中。
 5.2.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.3、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()在项目中经常会使用到的一些细节。
更多编程相关知识,请访问:编程视频课程!!
以上就是详解jquery中extend()和jquery.fn.extend()的区别的详细内容。
其它类似信息

推荐信息