http://blog.csdn.net/xuemoyao/article/details/19366755
1. Contains method for array
The array does not have a contains method, in reality the scenario is that sometimes we need to determine whether a value is
In this array, this time a contains method is very necessary, specifically implemented as follows:
Determine if an element is contained in an array
Array.prototype.contains = function (obj) {
var i = this.length;
while (i--) {
if (this[i] = = = obj) {
return true;
}
}
return false;
}
2. Contains method of string
The same problem also exists in the string type, in JS also has no one to determine whether a substring is wrapped
The method contained in the parent string is implemented as follows:
Whether the string contains a string
String.prototype.contains = function contains (string, substr, isignorecase) {
if (isignorecase) {
this = This.tolowercase ();
string = String.tolowercase ();
substr = Substr.tolowercase ();
}
var Startchar = substr.substring (0, 1);
var strLen = substr.length;
for (var j = 0; J < String.length-strlen + 1; j + +) {
if (String.charat (j) = = Startchar)//
{
if (String.substring (J, j + strLen) = = substr)//
{
return true;
}
}
}
return false;
}
3. Date AddDays, addMonths, Addyear, Format method
Familiar with C # friends, will be very familiar with and enjoy a series of convenient operation of the DateTime, in JS and
There is no time-related operation as convenient as in C #, and sometimes the associated interactions such as the addition and subtraction of time are used, and the date type is extended specifically as follows:
Add Days
Date.prototype.addDays = function (d) {
This.setdate (This.getdate () + D);
};
Add Week
Date.prototype.addWeeks = function (w) {
This.adddays (W * 7);
};
Add month
Date.prototype.addMonths = function (m) {
var d = this.getdate ();
This.setmonth (This.getmonth () + M);
if (This.getdate () < D)
This.setdate (0);
};
Add year
Date.prototype.addYears = function (y) {
var m = This.getmonth ();
This.setfullyear (This.getfullyear () + y);
if (M < This.getmonth ()) {
This.setdate (0);
}
};
Format processing of dates
Date formatting
Date.prototype.Format = function (FMT) {//author:meizz
var o = {
"m+": This.getmonth () + 1,//month
"D+": this.getdate (),//day
"H +": this.gethours (),//hour
"m+": this.getminutes (),//min
"S+": This.getseconds (),//sec
"q+": Math.floor ((This.getmonth () + 3)/3),//Quarterly
"S": this.getmilliseconds ()//MS
};
if (/(y+)/.test (FMT))
FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O)
if (New RegExp ("(" + K + ")"). Test (FMT))
FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));
return FMT;
};
4. Math.max.apply (Null,array), find the maximum value in the array
This method is mainly used to find the maximum value in an array, which is often encountered in the actual work. Or
A friend asked, why not call the Math.max () method directly? It is important to note that the Math.max () method supports the passing of multiple parameters, but it does not support passing an array directly as a parameter, but all functions have a method such as apply (scope, parameter), and we use the Apply method to indirectly set the array as an argument. And each value in the array is taken apart to pass to the Max method, thus achieving the need to find the maximum value.
JS basic data type common method extension