For some novice programmers, the most annoying thing is that they encapsulate the function, not only to achieve its original function, but also to encapsulate into a function, in order to facilitate the future writing code is more convenient, so every time after encapsulating a function, you can directly call when using it. Let me take you into my code world.
Encapsulate an isNaN function
Idea: Take the processed number, convert it to a string, and compare it to isNaN
function myIsNaN(n) { var num = Number(n); If (num + '=== 'NaN') {return true} return false}Copy the code
Encapsulate a myPush method
function myPush(ary,n){ ary[ary.length] =n; } var ary = [1, 2]; myPush(ary,222); console.log(ary);Copy the code
Encapsulates a method that fetches all of the element’s child nodes under an element
Function getChildren(ele){var ary=[]; for(var i=0; i<ele.childNodes.length; i++){ if(ele.childNodes[i].nodeType == 1 ){ ary.push(ele.childNodes[i]) } } return ary; }Copy the code
Encapsulate a function that uses splice to push pop unshift Shift
function mypush2(arr,... ABC){// Residual operator... In the function // ABC is an array that stores all items after the first <! -- var ary=arguments[0]; Arr. Splice (arr. Length,0... Return arr. Length; return arr. } var ary=[1,2] mypush2(ary,4,5,[6,7,8],9) function mypop(arr){return arr.splice(arr.leng-1,1)[0]} mypop2=aryCopy the code
Encapsulate a function, mypush, that returns the changed array length
function mypush(arr){ var ary=arguments[0]; For (var I =1; i<arguments.length; I ++){arr[arr.length]=arguments[I]} return arr.length} var ary=[1,2] mypush(ary,4,5,[6,7,8],9) console.log(ary); / / [1,2,4,5, [June,], 9]Copy the code
Encapsulate a pop of your own
function mypop(arr) {
var a=arr[arr.length-1]
arr.length--
return a
}
var ary = [1, 2, 3];
var res=mypop(ary);
console.log(ary,res)
Copy the code
Sort and reverse sort of arrays
- Sort (function(a,b)){function(a,b){return b-a; / / descending})
Encapsulate a myReverse method
-
How it works: Loop through the arR array, add each item to a new array by unshift, and return the new array.
function myReverse(arr) { var a = []; for (var i = 0; i < arr.length; i++) { var temp = arr[i]; a.unshift(temp) } return a; } var ary = [1,2,3,4]; var res2 = myReverse(ary); console.log(res2, ary); // This method does not change the original array. The new array is output backwards.Copy the code
Repackage a new myRevers2
- Principle: the first term and the last term change position, the second term and the penultimate term change position…. Function myRevers2(arr){for(var I =0; i
){ var temp = arr[i]; arr[i] = arr[arr.length-1-i]; Arr [I] = temp; arr[arr. Length -1- I] = temp; } return arr; } var res3=myRevers2(arr1) consloe.log(res3,arr1)
Encapsulate a method, as used with join(), concatenated to a string of specified characters, not through the menu, comma concatenated by default.
Var res2=ary. Join ('+') var ary = [1,2,3,4]; function myjoin(arr,str){ var s=''; For (var I =0; i<arr.length-1; i++){ s += arr[i] +str } return s } var res=myjoin(ary,'+');Copy the code