An example of JS extension method _javascript Skills

Source: Internet
Author: User
Tags getdate

This article illustrates the technique of JS extension method implementation. Share to everyone for your reference. The specific analysis is as follows:

The JS extension method is very similar to the C # extension method, and can be chained to the call, but also by the extension of a class to the implementation. This thing is very easy to use, if the pre-written method to put in a JS quote, then write JS will be very interesting.

An example is given below:

 
 

It's like telling yourself that there is such a thing;

Here's a nice JS extension to find:

Clear both sides of the space String.prototype.trim = function () {return this.replace (^\s*) | ( 
\s*$)/g, ""); 
}; 
  Merges multiple blanks into a blank String.prototype.ResetBlank = function () {var regEx =/\s+/g; 
Return This.replace (RegEx, '); 
 
}; 
  Keep number String.prototype.GetNum = function () {var regEx =/[^\d]/g; 
Return This.replace (RegEx, '); 
 
}; 
  Keep Chinese String.prototype.GetCN = function () {var regEx =/[^\u4e00-\u9fa5\uf900-\ufa2d]/g; 
Return This.replace (RegEx, '); 
 
}; String converted to Number String.prototype.ToInt = function () {return isNaN (the parseint (this))? This.tostring (): parseint (thi 
s); 
 
}; 
  Get byte length String.prototype.GetLen = function () {var regEx =/^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; 
  if (Regex.test (this)) {return this.length * 2; 
   else {var omatches = This.match (/[\x00-\xff]/g); 
   var olength = this.length * 2-OMATCHES.LENGTH; 
  return olength; 
 
} 
}; Get file Full Name String.prototype.GetFileName = function () {var regEx =/^.*\/([^\/\?] *).*$/; 
Return This.replace (regEx, ' $ '); 
 
}; Gets the file name extension String.prototype.GetExtensionName = function () {var regEx =/^.*\/[^\/]* (\.[ ^\.\?] 
  *).*$/; 
Return This.replace (regEx, ' $ '); 
 
}; Replace All String.prototype.replaceAll = function (Reallydo, replacewith, ignoreCase) {if (!) RegExp.prototype.isPrototypeOf (Reallydo)) {return this.replace (new RegExp Reallydo, ignoreCase? 
  "GI": "G")), replacewith); 
  else {return This.replace (Reallydo, replacewith); 
} 
}; 
  format string add by Liu Jingning 2010-12-09 String.Format = function () {if (arguments.length = = 0) {return '; 
  } if (arguments.length = = 1) {return arguments[0]; var reg =/{(\d+)?} 
  /g; 
  var args = arguments; 
  var result = Arguments[0].replace (Reg, Function ($, $) {return Args[parseint ($) + 1]; 
  }); 
return result; 
 
}; 
  Digital complement 0 Number.prototype.LenWithZero = function (ocount) {var strText = this.tostring (); while (Strtext.length < ocount) {StrText = ' 0 '+ StrText; 
return strText; 
 
}; 
Unicode Restore Number.prototype.ChrW = function () {return string.fromcharcode (this); 
 
}; 
  Numeric arrays are sorted from small to large Array.prototype.Min2Max = function () {var ovalue; for (var i = 0; i < this.length. i++) {for (var j = 0; J <= i; j +) {if (This[i] < this[j]) {OVal 
    UE = This[i]; 
    This[i] = This[j]; 
   THIS[J] = Ovalue; 
}} return this; 
 
}; 
  The numeric array is sorted from large to small Array.prototype.Max2Min = function () {var ovalue; for (var i = 0; i < this.length. i++) {for (var j = 0; J <= i; j +) {if (This[i] > This[j]) {OVal 
    UE = This[i]; 
    This[i] = This[j]; 
   THIS[J] = Ovalue; 
}} return this; 
 
}; 
  Gets the maximum item Array.prototype.GetMax = function () {var ovalue = 0 in a numeric array; 
   for (var i = 0; i < this.length i++) {if (This[i] > Ovalue) {ovalue = this[i]; 
} return ovalue; 
 
}; Gets the minimum Item Array.prototype.GetMin = function () {var ovalue in a numeric array = 0; 
   for (var i = 0; i < this.length i++) {if (This[i] < Ovalue) {ovalue = this[i]; 
} return ovalue; 
 
}; 
  Gets the Chinese form of the current time Date.prototype.GetCNDate = function () {var odatetext = '; Odatetext + = This.getfullyear (). Lenwithzero (4) + new number (24180). 
  ChrW (); Odatetext + = This.getmonth (). Lenwithzero (2) + new number (26376). 
  ChrW (); Odatetext + = This.getdate (). Lenwithzero (2) + new number (26085). 
  ChrW (); Odatetext + = This.gethours (). Lenwithzero (2) + new number (26102). 
  ChrW (); Odatetext + = This.getminutes (). Lenwithzero (2) + new number (20998). 
  ChrW (); Odatetext + = This.getseconds (). 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 (This.getday () * 5, 5). ToInt (). 
  ChrW (); 
return odatetext; 
}; Extended Date Format Date.prototype.Format = function (format) {var o = {"m+": THIS.GEtmonth () + 1,//month "d+": this.getdate (),//day "h+": this.gethours ()% 12 = 0? 12:this.gethours ()% 12,//h "h+": this.gethours (),//Hour "m+": this.getminutes (),//Cent "s+": This.getseco 
  NDS (),//sec "q+": Math.floor (This.getmonth () + 3)/3),//Quarter "S": this.getmilliseconds ()//millisecond}; var week = {"0": "\u65e5", "1": "\u4e00", "2": "\u4e8c", "3": "\u4e09", "4": "\u56db", "5": "\u4e 
  "", "6": "\U516D"};  
  if (/(y+)/.test (format)) {format = Format.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); } if (/(e+)/.test (format)) {format = Format.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) 
  "\u661f\u671f": "\u5468"): "") + week[this.getday () + ""]); For (var k in O) {if (New RegExp ("+ K +")). Test (format) {format = Format.replace (regexp.$1, regexp.$ 1.length = 1)? 
   (O[k]): (("+ o[k]"). substr (("" + o[k). length));} return format; } Date.prototype.Diff = function (interval, objdate) {//if parameter is insufficient or objdate is not a date type return undefined if (arguments.length ; 2 | | 
  Objdate.constructor!= Date) {return undefined;} 
   Switch (interval) {//seconds difference case ' s ': Return parseint ((objdate-this)/1000); 
   Calculate the difference case ' n ': Return parseint ((objdate-this)/60000); 
   Calculate time difference case ' h ': Return parseint ((objdate-this)/3600000); 
   Calculate the day difference case ' d ': Return parseint ((objdate-this)/86400000); 
   Calculates the week difference case ' W ': Return parseint ((objdate-this)/(86400000 * 7)); Calculate month difference case ' m ': Return (Objdate.getmonth () + 1) + ((Objdate.getfullyear ()-this.getfullyear ())-(This.getmont 
   H () + 1); 
   Calculate the year difference case ' y ': return objdate.getfullyear ()-this.getfullyear (); 
  Input error default:return undefined; 
 
} 
}; 
Detection is null Object.prototype.IsNullOrEmpty = function () {var obj = this; 
var flag = false; if (obj = null | | obj = undefined | | typeof (obj) = = ' Undefined ' | | 
obj = = ') {flag = true; 
   else if (typeof (obj) = = ' string ') {obj = Obj.trim (); 
   if (obj = =) {//is empty flag = true; 
   else {//is not null obj = Obj.touppercase (); 
   if (obj = = ' NULL ' | | | obj = ' UNDEFINED ' | | | obj = = ' {} ') {flag = true; 
}} else {flag = false; return flag;

The

wants this article to help you with your JavaScript programming.

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.