Continue to encapsulate the module functionality of the jquery framework

Source: Internet
Author: User

The function of the framework selector module has been encapsulated in the first two days, in order to maintain the integrity of the code and to embody the functionality of the Framework module. Here, a H5 method is used to simply represent the selector module.

1(function(window) {//The window Global object is passed in to reduce the depth of access to the scope2      //1. Selector module3         varselect = (function () {4             return function(Selector, context) {5                 if(context) {6                     returnContext.queryselectorall (selector);7}Else {8                     returnDocument.queryselectorall (selector);9                 }Ten             } One         }()); A      //2. Entry function of frame -      functionItcast (selector,context) { -     return NewItcast.fn.init (selector,context); the      } -  -    varpush = Array.prototype.push;//Save the Push method of the array - //3. Adding a prototype attribute method +Itcast.fn = itcast.prototype={ - Constructor:itcast, +Splice:Array.prototype.splice,//saves the splice method of the array as a property of the current object, which makes it easy to invoke AInit:function(selector,context) { at         if(Selector = =NULL)return  This; -         //Use the splice method of the array to empty the original element -          This. Splice.call ( This, 0, This. length); -         if(typeofselector = = = "string"){ -            if(Selector.charat (0) = = = "<" && Selector.charat (selector.length-1) = = = ">" && selector.length> =3){ -                  //The case that selector is a string with a label in                  vardiv = document.createelement ("div"); -                  //Div is actually as an intermediate amount, convenient to store elements in a pseudo-array form into the Init object todiv.innerhtml =selector; +Push.apply ( This, div.childnodes); -}Else{ thePush.apply ( This, select (Selector,context); *            }     $}Else if(Selector.nodetype) {//Selector is the case of a DOM elementPanax Notoginseng              This[0] =selector; -              This. length =1; the         } +         return  This;//returns the current Init object for chained programming A     } the }; +  - //4. Change the INIT function's prototype object below, so that the instance object of Init can access the method in Incast.fn $Itcast.fn.init.prototype =Itcast.fn; $  - //5. Encapsulate a extend method for subsequent extension methods - //The main function of the Extend method is to complete the copy and copy the methods and properties from one object to another . theItcast.fn.extend = Itcast.extend =function(){ -     varTarget//target ObjectWuyi     varSources =[];//Source Object the     if(arguments.length===1){ -target = This; WuSources.push (arguments[0]); -}Else{ Abouttarget = Arguments[0]; $ sources.push.apply (sources,arguments); -Sources.splice (0,1);//Remove the first element because the first element is the target object -     } -      for(vari=0;i<sources.length;i++){ A         varSource =Sources[i]; +          for(varKeyinchsource) { thetarget[key]=Source[key]; -         } $     } the     returnTarget//returns the target object after the copy the } the}) (window)

Oh! It's 12 o'clock, so write it today.

Continue to encapsulate the module functionality of the jquery framework

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.