$.extend () Realization of source code-(source learning 1)

Source: Internet
Author: User

Target: $.extend ({ Add:function (A, b) { return a + B;         }     }) Console.log ($.add);How is it possible to hang the add function behind jquery?jquery-v1.12.4
1Jquery.extend = JQuery.fn.extend =function() {2     varsrc, copyisarray, copy, name, options, clone,3target = arguments[0] | | {},4i = 1,5Length =Arguments.length,6Deep =false;7 8     //Handle a deep copy situation9     if(typeoftarget = = = "Boolean" ) {TenDeep =Target; One  A         //Skip the Boolean and the target -target = arguments[I] | | {}; -i++; the     } -  -     //Handle Case If Target is a string or something (possible in deep copy) -     if(typeofTarget!== "Object" &&!jquery.isfunction (target)) { +target = {}; -     } +  A     //extend JQuery itself if only one argument is passed at     if(i = = =length) { -target = This; -i--; -     } -  -      for(; i < length; i++ ) { in  -         //Only deal with non-null/undefined values to         if(options = arguments[i])! =NULL ) { +  -             //Extend the Base object the              for(Nameinchoptions) { *src =target[name]; $copy =options[name];Panax Notoginseng  -                 //Prevent Never-Ending Loop the                 if(target = = =copy) { +                     Continue; A                 } the  +                 //Recurse If we ' re merging plain objects or arrays -                 if(Deep && copy && jquery.isplainobject (copy) | | $(Copyisarray =jquery.isarray (copy))) ) { $  -                     if(Copyisarray) { -Copyisarray =false; theclone = src && jquery.isarray (src)?src: []; - Wuyi}Else { theclone = src && jquery.isplainobject (src)?src: {}; -                     } Wu  -                     //never move original objects, clone them Abouttarget[name] =jquery.extend (deep, clone, copy); $  -                 //Don ' t bring in undefined values -}Else if(Copy!==undefined) { -target[name] =copy; A                 } +             } the         } -     } $  the     //Return the Modified object the     returnTarget; the};

Analysis:Line 3rd: Take the function add第23-26: Assigns this, the instance object of jquery, function (selector, context) {} to target.Line 31st: Assign the function add to the options and iterate. line 36th: options = {Add:function (A, b) {...}} Assign a value to copy.line 60th: by Target[name]=copy; Adding a property to the jquery instance adds the jquery[' Add ']=function (A, b) {return a+b};

$.extend () Realization of source code-(source learning 1)

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.