jquery source Note (iv): Jquery.extend=jquery.fn.extend

Source: Internet
Author: User

The Extend () method merges the properties of two or more objects into the first object, and most of the subsequent functions of jquery are extended through the function,

When the argument has only one object, the object's properties are added to the jquery object.

The form of extension in jquery: 2 ways

$.extend ({//Extension Tool MethodsAaa:function() {alert (10); }, BBB:function() {alert (20)        }    }); $.fn.extend ({//extending the jquery instance approachAaa:function() {alert ("FN 1"); }, BBB:function() {alert ("FN 2");    }    }); $.extend ();  This-$ This. AAA-$.aaa ();   $.fn.extend ();  This$.fn (prototype) This. AAA--$ (). AAA ()

When there are multiple objects in the argument, the subsequent objects are extended to the first object:

    var a = {name: "Linda"};    $.extend (a,{name:"Joke", Age:20},{address: "China"});    Console.log (a);         //     {name= "Joke",  age=20,  address= "China"}

With deep and shallow copies, jquery is a shallow copy by default

Shallow copy:varA = {name: "Linda"}; varb = {firends:[' aa ', ' BB ', ' CC ']};    $.extend (A, b); A.firends.push (' DD ');        Console.log (b); //{firends:[' aa ', ' BB ', ' cc ', ' DD '}Deep Copy:varA = {name: "Linda"}; varb = {firends:[' aa ', ' BB ', ' CC ']}; $.extend (true , A,b); The first parameter is set to True, which is a deep copy of A.firends.push (' DD ');        At this point the modification of the reference type of a has no effect on B console.log (b); //{firends:[' aa ', ' BB ', ' cc ']}

Decomposition of the source code:

function () {    define some variables
if () {} See if it is a deep copy case
if () {} See if the parameters are correct
if () {} see if there is a plug-in condition for () { There may be multiple object conditions
if () {} prevents circular references
if () {} deep copy
else if () {} Shallow copy
}

jquery source Note (iv): Jquery.extend=jquery.fn.extend

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.