This article mainly introduces the various function encapsulation methods of class in js, and introduces the encapsulation methods of different functions, if you are interested, refer to the examples in this article to explain various function encapsulation methods of class in js and share them with you for your reference. The specific content is as follows:
Function encapsulation of Classes
Script // The indexOf method of the array encapsulates function indexOf (arr, value, start) {// if start is not set, the default start is 0 if (arguments. length = 2) {start = 0;} // if the indexOf method exists in the array, use the native indexOf method if (arr. indexOf) {return arr. indexOf (value, start) ;}for (var I = 0; I <arr. length; I ++) {if (arr [I] === value) {return I ;}} return-1 ;} // function noRepeat (arr) {var result = []; for (var I = 0; I <arr. length; I ++) {if (indexOf (result, Arr [I]) =-1) {result. push (arr [I]) ;}} return result ;}// the inArray method encapsulates function inArray (arr, value) {for (var I = 0; I <arr. length; I ++) {if (arr [I] = value) {return true ;}return false ;}// remove the first and last spaces. function trim (arr) is encapsulated) {var result = arr. replace (/^ \ s + | \ s + $/g, ''); return result;} // The getElementsByClassName function encapsulates function getElementsByClassName (parentObj, classStr) {var result = []; var objs = parentObj. get ElementsByTagName ('*'); // If classStr is separated by spaces, it indicates that the class must meet both conditions before var targetArr1 = noRepeat (trim (classStr) is valid ). split (/\ s +/); // If classStr is separated by commas, it indicates that the class is valid as long as one of the values is met. var targetArr2 = noRepeat (trim (classStr ). split (/\ s *, \ s */); if (classStr. indexOf (',') =-1) {// use spaces to separate or only one class label: for (var I = 0; I <objs. length; I ++) {var arr = noRepeat (trim (objs [I]. className ). split (/\ s +/); for (var j = 0; j <targetArr1. Length; j ++) {if (! InArray (arr, targetArr1 [j]) {continue label ;}} result. push (objs [I]);} return result;} else {// use a comma to separate the label: for (var I = 0; I <objs. length; I ++) {var arr = noRepeat (trim (objs [I]. className ). split (/\ s +/); for (var j = 0; j <targetArr2.length; j ++) {if (inArray (arr, targetArr2 [j]) {result. push (objs [I]); continue label ;}} return result ;}// the addclass function encapsulates function addClass (obj, classStr) {var array = NoRepeat (trim (obj. className). split ('\ s +'); if (! InArray (array, classStr) {array. push (classStr);} obj. className = array. join (''); return obj;} // The removeclass function encapsulates function removeClass (obj, classStr) {var array = noRepeat (trim (obj. className ). split ('\ s +'); var index = indexOf (array, classStr); if (index! =-1) {classStr. splice (index, 1); obj. className = classStr. join ('');} return obj;} // The toggleClass function encapsulates function toggleClass (obj, classStr) {var array = noRepeat (trim (obj. className ). split ('\ s +'); if (inArray (array, classStr) {removeClass (obj, classStr);} else {addClass (obj, classStr );}} script
I hope this article will help you learn about javascript programming.