Welcome to visit my github:huanshen, have my source code analysis
1. Each
It's much like a for loop, but it's more useful, and you know it if you understand it.
//iterate over an array or object //obj is an array or an object that needs to be traversed //callback is a callback function that handles each element of an array/object, and its return value actually interrupts the process of looping //args is an additional array of argumentseachfunction(obj, callback, args) {varvalue, I= 0, Length=Obj.length, IsArray=isarraylike (obj); //If args exists, the parameter passed in the callback is the Grgs,each loop number, which is determined by obj . if(args) {if(IsArray) { for(; i < length; i++) {Value=callback.apply (obj[i], args); if(Value = = =false ) { Break; } } } Else { for(Iinchobj) {Value=callback.apply (obj[i], args); if(Value = = =false ) { Break; } } } //A Special, fast, case for the most common use of each //Note that the following is call, which is apply //the second parameter of apply is passed in as an array, but when it is actually running, //The passed-in array parameter becomes a form, not an array parameter}Else { if(IsArray) { for(; i < length; i++) {Value=Callback.call (obj[i], I, obj[i]); if(Value = = =false ) { Break; } } } Else { for(Iinchobj) {Value=Callback.call (obj[i], I, obj[i]); if(Value = = =false ) { Break; } } } } returnobj; },2.makeArray
Converts an array of classes object to a group. The merge function is used in the middle, the third function is visible, and there is a description.
//to convert a class array object to a group object //This method is an internal methodMakearray:function(arr, results) {varret = Results | | []; if(Arr! =NULL ) { //If arr is a class array object, call merge to return a value //still not very understanding isarraylike and merge if(Isarraylike (Object (arr))) {Jquery.merge (ret),typeofarr = = = "string"?[arr]: arr); } Else { //if it is not an array, drop it at the end of the returned array //equivalent to Ret.push (arr);Core_push.call (ret, arr); } } returnret; },
About the middle of the object () can look at the following results (temporarily not very clear specific role):
var str= "Tttt", arr1=[1,2,3], obj={1:0}, str2=Object (str); Console.log (Object (str))//{0: "T", 1: "T", 2: "T", 3: "T", length:4, [[[Primitivevalue]]: "TTTT"} Console.log (str2.length)//4 console.log (Object (arr1))//[1, 2, 3] console.log (Object (obj))//{1:0}
3. Merge
You can merge two arrays or class array objects
//add attributes from second to first //second can be an array or a class array object, or something that contains 0, 1 propertiesMergefunction(first, second) {varL =Second.length, I=First.length, J= 0; if(typeofL = = = "Number" ) { for(; J < L; J + +) {first[i++ ] =second[J]; } } Else { while(Second[j]!==undefined) {first[I+ +] = second[J + + ]; }} first.length=i; returnFirst ; },4. Map
function is similar to each, but there are different places, mainly in dealing with the third parameter.
Mapfunction(Elems, Callback, ARG) {varvalue, I= 0, Length=Elems.length, IsArray=Isarraylike (elems), ret= []; //Go through the array, translating each of the items to their //if it is an array, isarray=true; //is consistent with each passed in parameter order if(IsArray) { for(; i < length; i++) {Value=callback (elems[i], I, ARG); if(Value! =NULL) {ret[ret.length]=value; } } //Go through every key on the object, //if the object}Else { for(IinchElems) {Value=callback (elems[i], I, ARG); if(Value! =NULL) {ret[ret.length]=value; } } } //Flatten any nested arrays returncore_concat.apply ([], ret); },5. Proxy
Proxy (), accept a function, and return a new function, and the new function always maintains a specific context.
Proxyfunction(FN, context) {vartmp, args, proxy; if(typeofContext = = = "string") {tmp=fn[context]; Context=fn; FN=tmp; } //Quick Check to determine if target are callable, in the spec //This throws a TypeError, but we'll just return undefined. if( !jquery.isfunction (FN)) { returnundefined; } //simulated bind //If the parameter passed in is 2 extra, the extra parameter is converted to an array. args = Core_slice.call (arguments, 2 ); Proxy=function() { //currying is used here. returnFn.apply (Context | | This, Args.concat (core_slice.call (arguments))); }; //Set the GUID of unique handler to the same of original handler, so it can be removedProxy.guid = Fn.guid = Fn.guid | | jquery.guid++; returnproxy; },
Currying is a technique that transforms a function that accepts multiple parameters into a function that takes a single parameter (the first parameter of the original function) and returns a new function that takes the remaining parameters and returns the result. Here's an example: you can see that the previous two parameters have no output, and the last parameter and the new parameter make up the array output. In the above is passed as a parameter in the proxy
function T (l,t,y) { var args = [].slice.call (arguments, 2 ); Console.log (args) function t () { console.log (Args.concat ([].slice.call (arguments))); } return t; } var tt=t (N/a); TT ("T"); // [3, "T"]
Summary of functions in jquery 1