Home  >  Article  >  Web Front-end  >  Detailed explanation of the difference between extend() and jQuery.fn.extend() in jQuery

Detailed explanation of the difference between extend() and jQuery.fn.extend() in jQuery

青灯夜游
青灯夜游forward
2020-11-26 17:59:103029browse

Detailed explanation of the difference between extend() and jQuery.fn.extend() in jQuery

Related recommendations: "jQuery Video Tutorial"

1. Get to know jQuery extend( ) and jQuery.fn.extend()

jQuery’s API manual, the extend method is mounted in jQuery and jQuery.fn have methods on two different objects, but the internal code of jQuery implements the same, but the functions are different;

Let’s see the official explanation:

jQuery.extend(): Merge the contents of two or more objects together into the first object.(Merge two or more objects into the first one);

jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(Mount the object Go to jQuery's prototype attribute to extend a new jQuery instance method )

2. Understand jQuery.extend()

Let’s first treat jQuery as a class, so it’s easier to understand. jQuery.extend() is an extended jQuery class.

Suppose we regard the jQuery class as a human being, who can eat, drink, run and jump. Now we use the jQuery.extend method to extend this class with a speak( ) skills. In this case, whether you are a man, a woman, a xx person...etc., you can inherit this skill (method).

can be written as follows:

JQuery.extend({
    speak:function(){
         alert("how are you!");
    }
});

The calling method is as follows:

<!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>

This means that $.speak) has become a method (object) of the jQuery class itself, and he can now "speak".

But, this ability can only be used by the jQuery class itself, which represents all mankind. If you personally want to use it, you Zhang San Li Si Wang Wu Ma Liu, you little grassroots can represent all mankind?

So, this extension is the so-called static method, which is only related to the class itself. It has nothing to do with your specific instantiated object.

3. Understanding jQuery.fn.extend()

Literally understood, this extension is the method of jQuery.fn. What is jQuery.fn?

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

So jQuery.fn.extend extends the method of the jQuery object (prototype)!

What is the object? It is the instantiation of a class, such as $("#abc"), $(p)

That is to say, the jQuery.fn.extend extension method must be used in jQuery Only on top of the object! He has to be an instantiated object such as Zhang, San, Li, Si, Wang, Wu, and Liu to be able to use it.

To put it bluntly, it has to be used like this (assuming xyz() is an expansion method):

$('selector').xyz( );

The calling method is as follows:

<!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. Summary of the difference between the two:

4.1. The two calling methods are different:

       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()在项目中经常会使用到的一些细节。

更多编程相关知识,请访问:编程视频课程!!

The above is the detailed content of Detailed explanation of the difference between extend() and jQuery.fn.extend() in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete