SoFunction
Updated on 2025-04-09

JS extension method example analysis

This article describes the implementation techniques of JS extension method. Share it for your reference. The specific analysis is as follows:

The JS extension method is very similar to the C# extension method. It can also be called in a chain and is also implemented through the extension writing method of a certain class. This thing is very useful. If you put the pre-written method into a js reference, then writing js later will be very interesting.

Here is an example:

<head>
  <title>testJSExtended Method</title>
  <script type="text/javascript">
   // Merge multiple blanks into one blank    = function() { //Extend string   var regEx = /\s+/g; 
   return (regEx, ' '); 
   }; 
    = function()
   {
   var str = "Are you okay if he wants to?";
   alert(str);
   str = (); //This way you can call it, and C# is very similar! alert(str);   }
  </script>
</head>

It seems to be just telling myself that there is such a thing;

Here is a very good js extension found:

// Clear spaces on both sides = function() { 
  return (/(^\s*)|(\s*$)/g, ''); 
}; 
// Merge multiple blanks into one blank = function() { 
  var regEx = /\s+/g; 
  return (regEx, ' '); 
}; 
 
// Keep the numbers = function() { 
  var regEx = /[^\d]/g; 
  return (regEx, ''); 
}; 
 
// Keep in Chinese = function() { 
  var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; 
  return (regEx, ''); 
}; 
 
// Convert String to Number = function() { 
  return isNaN(parseInt(this)) ? () : parseInt(this); 
}; 
 
// Get the byte length = function() { 
  var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; 
  if ((this)) { 
   return  * 2; 
  } else { 
   var oMatches = (/[\x00-\xff]/g); 
   var oLength =  * 2 - ; 
   return oLength; 
  } 
}; 
 
// Get the full name of the file = function() { 
  var regEx = /^.*\/([^\/\?]*).*$/; 
  return (regEx, '$1'); 
}; 
 
// Get the file extension = function() { 
  var regEx = /^.*\/[^\/]*(\.[^\.\?]*).*$/; 
  return (regEx, '$1'); 
}; 
 
//Replace all = function(reallyDo, replaceWith, ignoreCase) 
{ 
  if (!(reallyDo)) { 
   return (new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith); 
  } else { 
   return (reallyDo, replaceWith); 
  } 
}; 
//Format string add By Liu Jingning 2010-12-09 = function() { 
  if ( == 0) { 
   return ''; 
  } 
 
  if ( == 1) { 
   return arguments[0]; 
  } 
 
  var reg = /{(\d+)?}/g; 
  var args = arguments; 
  var result = arguments[0].replace(reg, function($0, $1) { 
   return args[parseInt($1) + 1]; 
  }); 
  return result; 
}; 
 
// Numbers to zero = function(oCount) { 
  var strText = (); 
  while ( < oCount) { 
   strText = '0' + strText; 
  } 
  return strText; 
}; 
 
// Unicode restore = function() { 
  return (this); 
}; 
 
// Array of numbers is sorted from small to large.Min2Max = function() { 
  var oValue; 
  for (var i = 0; i < ; i++) { 
   for (var j = 0; j <= i; j++) { 
   if (this[i] < this[j]) { 
    oValue = this[i]; 
    this[i] = this[j]; 
    this[j] = oValue; 
   } 
   } 
  } 
  return this; 
}; 
 
// Array of numbers is sorted from large to small.Max2Min = function() { 
  var oValue; 
  for (var i = 0; i < ; i++) { 
   for (var j = 0; j <= i; j++) { 
   if (this[i] > this[j]) { 
    oValue = this[i]; 
    this[i] = this[j]; 
    this[j] = oValue; 
   } 
   } 
  } 
  return this; 
}; 
 
// Get the largest item in the numeric array = function() { 
  var oValue = 0; 
  for (var i = 0; i < ; i++) { 
   if (this[i] > oValue) { 
   oValue = this[i]; 
   } 
  } 
  return oValue; 
}; 
 
// Get the smallest term in the array of numbers = function() { 
  var oValue = 0; 
  for (var i = 0; i < ; i++) { 
   if (this[i] < oValue) { 
   oValue = this[i]; 
   } 
  } 
  return oValue; 
}; 
 
// Get the Chinese form of the current time = function() { 
  var oDateText = ''; 
  oDateText += ().LenWithZero(4) + new Number(24180).ChrW(); 
  oDateText += ().LenWithZero(2) + new Number(26376).ChrW(); 
  oDateText += ().LenWithZero(2) + new Number(26085).ChrW(); 
  oDateText += ().LenWithZero(2) + new Number(26102).ChrW(); 
  oDateText += ().LenWithZero(2) + new Number(20998).ChrW(); 
  oDateText += ().LenWithZero(2) + new Number(31186).ChrW(); 
  oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String('26085199682010819977222352011620845').substr(() * 5, 5).ToInt().ChrW(); 
  return oDateText; 
}; 
//Extended Date formatting = function(format) { 
  var o = { 
   "M+": () + 1, //month   "d+": (), //day   "h+": () % 12 == 0 ? 12 : () % 12, //Hour   "H+": (), //Hour   "m+": (), //point   "s+": (), //Second   "q+": ((() + 3) / 3), //Quarterly   "S": () //millisecond  }; 
  var week = { 
   "0": "\u65e5", 
   "1": "\u4e00", 
   "2": "\u4e8c", 
   "3": "\u4e09", 
   "4": "\u56db", 
   "5": "\u4e94", 
   "6": "\u516d" 
  }; 
  if (/(y+)/.test(format)) { 
   format = (RegExp.$1, (() + "").substr(4 - RegExp.$)); 
  } 
  if (/(E+)/.test(format)) { 
   format = (RegExp.$1, ((RegExp.$ > 1) ? (RegExp.$ > 2 ? "\u661f\u671f" : "\u5468") : "") + week[() + ""]); 
  } 
  for (var k in o) { 
   if (new RegExp("(" + k + ")").test(format)) { 
   format = (RegExp.$1, (RegExp.$ == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); 
   } 
  } 
  return format; 
} 
 = function(interval, objDate) { 
  //If the parameters are insufficient or objDate is not the date type, return undefined  if ( < 2 ||  != Date) { return undefined; } 
  switch (interval) { 
   // Calculate the second difference   case 's': return parseInt((objDate - this) / 1000); 
   // Calculate the score difference   case 'n': return parseInt((objDate - this) / 60000); 
   // Calculation time difference   case 'h': return parseInt((objDate - this) / 3600000); 
   // Calculate the daily difference   case 'd': return parseInt((objDate - this) / 86400000); 
   // Calculate the weekly difference   case 'w': return parseInt((objDate - this) / (86400000 * 7)); 
   // Calculate the monthly difference   case 'm': return (() + 1) + ((() - ()) * 12) - (() + 1); 
   //Calculate the year difference   case 'y': return () - (); 
   //The input is incorrect   default: return undefined; 
  } 
}; 
 
//Detect whether it is empty = function() { 
var obj = this; 
var flag = false; 
if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') { 
   flag = true; 
} else if (typeof (obj) == 'string') { 
   obj = (); 
   if (obj == '') {//Empty   flag = true; 
   } else {//Not empty   obj = (); 
   if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') { 
    flag = true; 
   } 
   } 
} 
else { 
   flag = false; 
} 
return flag;

I hope this article will be helpful to everyone's JavaScript programming.