The $.extend and $.fn.extend roles and differences of jquery, and some of their small details

Source: Internet
Author: User

$.extend (obj); to extend the jquery itself, add a new method to the class
$.fn.extend (obj); Adds a method to a jquery object.
such as (1):

   $.extend ({        add:function(b) {            return +b;        }    })    $.add (//return  

Note that there is no direct call on this side of the call, without any objects in front. Direct $.+ Method Name

(2):

 $.fn.extend ({clickwhile:  function   () {$ ( this ). Click (function   this  
    
     ). val ())})}) $ (
     ' input '). Clickwhile (); //  

Note that there are objects in front of the call. That is $ (' input ') such a thing.

describes their essential differences before, and now specifically describes their respective details
actually $.extend () has two ways of writing
(1) $.extend (obj);
         This is only one parameter, which means: Adding a class method to the jquery class can be understood as adding a static method, which is the global method.
(2) $.extend (target, object1,  object2,  object3,  object4,[objectn]);
         This is two or more than two arguments that represent the meaning of extending an object with one or more other objects, returning the extended object. That is, the first object is expanded and overwritten.
      

(function  ($) {            var aaa={name: ' 1 ', Age: ' + ', Sex: ' NV '};             var bbb={name: ' 2 ', Age: ' + '};             var ccc = $.extend (AAA, BBB);            alert (ccc.sex); // NV            alert (aaa.name); // 2        }) (JQuery);

I wonder if you have seen $.extend ({}, AAA, BBB) with {}? Take a look below

(function  ($) {            var aaa={name: ' 1 ', Age: ' + ', Sex: ' NV '};             var bbb={name: ' 2 ', Age: ' + '};             var ccc = $.extend ({}, AAA, BBB);            alert (ccc.sex); // NV            alert (aaa.name); // 1        }) (JQuery);

Why are alert (aaa.name) different results? If you knew, you wouldn't have to look down.
This is because: when multiple object parameters are merged, the structure of the first object is destroyed, so you can pass an empty object as the first argument

$.fn.extend (obj);
What does the FN in $.fn mean?
Actually is prototype, namely $.FN=$.PROTOYTPE;
If you know the prototype, you'll see what he means.

JQuery's $.extend and $.fn.extend roles and differences, and some of their small details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.