Home > Web Front-end > JS Tutorial > JavaScript common function library based on prototype extension_javascript skills

JavaScript common function library based on prototype extension_javascript skills

WBOY
Release: 2016-05-16 18:14:54
Original
1168 people have browsed it
Copy code The code is as follows:

/**
2 * Retrieve array elements (prototype extension or overloading)
3 * @param {o} The retrieved element value
4 * @type int
5 * @returns Element index
6*/
7 Array.prototype.contains = function(o) {
8 var index = -1;
9 for(var i=0;ireturn index;
}

/**
* Date formatting (prototype extension or overload)
* Format YYYY/yyyy/YY/yy represents year
* MM/M month
* W/w week
* dd/ DD/d/D date
* hh/HH/h/H time
* mm/m minute
* ss/SS/s/S second
* @param {formatStr} format template
* @type string
* @returns date string
*/
Date.prototype.format = function(formatStr){
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' (this.getYear() % 100));
str=str.replace(/MM/,(this.getMonth() 1)>9?(this.getMonth() 1).toString():'0' (this.getMonth() 1));
str=str.replace(/M/g,this.getMonth());
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
return str;
}

/**
* Compare date differences (prototype extension or overload)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {dtEnd} The format is date type or valid date format string
* @type int
* @returns comparison result
*/
Date.prototype.dateDiff = function(strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string' ) { //如果是字符串转换为日期型
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm' :return (dtEnd.getMonth() 1) ((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth() 1);
case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
}
}

/**
* Date calculation (prototype extension or overload)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {Number} quantity
* @type Date
* @returns calculated date
*/
Date.prototype.dateAdd = function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(Date.parse(dtTmp) (1000 * Number));
case 'n' :return new Date(Date.parse(dtTmp) (60000 * Number));
case 'h' :return new Date(Date.parse(dtTmp) (3600000 * Number));
case 'd' :return new Date(Date.parse(dtTmp) (86400000 * Number));
case 'w' :return new Date(Date.parse(dtTmp) ((86400000 * 7) * Number));
case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'y' :return new Date((dtTmp.getFullYear() Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
}
}

/**
* Get date data information (prototype extension or overloading)
* @param {interval} Date type: 'y, m, d, h, n, s, w'
* @type int
* @returns the specified date part
*/
Date.prototype.datePart = function(interval){
var myDate = this;
var partStr='';
var Week = ['日','一','二','三','四','五','六'];
switch (interval)
{
case 'y' :partStr = myDate.getFullYear();break;
case 'm' :partStr = myDate.getMonth() 1;break;
case 'd' :partStr = myDate.getDate();break;
case 'w' :partStr = Week[myDate.getDay()];break;
case 'ww' :partStr = myDate.WeekNumOfYear();break;
case 'h' :partStr = myDate.getHours();break;
case 'n' :partStr = myDate.getMinutes();break;
case 's' :partStr = myDate.getSeconds();break;
}
return partStr;
}

/**
* 날짜를 배열로 분할(프로토타입 확장 또는 오버로드)
* @type array
* @returns 날짜 배열
*/
Date.prototype.toArray = function() {
var myDate = this;
var myArray = 배열();
myArray[0] = myDate.getFullYear();
myArray[1] = myDate.getMonth() 1;
myArray[2] = myDate.getDate();
myArray[3] = myDate.getHours();
myArray[4] = myDate.getMinutes();
myArray[5] = myDate.getSeconds();
myArray를 반환합니다.
}

/**
* 이번 달의 일수 가져오기(프로토타입 확장 또는 오버로드)
* @type int
* @일수를 반환
*/
Date.prototype.daysOfMonth = function(){
var myDate = this;
var ary = myDate.toArray();
var date1 = (new Date(ary[0],ary[1] 1,1));
var date2 = date1.dateAdd('m',1);
var result = daysDiff(date1.format('yyyy-MM-dd'),date2.format('yyyy-MM-dd'));
반환 결과;
}

/**
* 윤년 결정(프로토타입 확장 또는 오버로딩)
* @type boolean
* @윤년인지 여부를 반환 true/false
*/
Date.prototype.isLeapYear = function() {
return (0==this.getYear()%4&&((this. getYear() 0!=0)||(this.getYear()@0==0)));
}

/**
* 두 날짜 간의 일수 차이 비교(사용자 지정)
* @param {DateOne} 1일
* @param {DateOne} 2일
* @type int
* @returns 비교결과
*/
함수 daysDiff(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('- '));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-') 1);
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));

var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-') 1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));

var cha=((Date.parse(OneMonth '/' OneDay '/' OneYear)- Date.parse(TwoMonth '/' TwoDay '/' TwoYear))/86400000);
return Math.abs(cha);
}

/**
* 날짜 계산(사용자 정의)
* @param {strInterval} 날짜 유형: 'y, m, d, h, n, s, w'
* @param {Number} 수량
* @param {prmDate} 원래 날짜
* @type 날짜
* @returns 계산된 날짜
*/
function dateAdd(interval,number,prmDate){
number = parseInt(number);
if (typeof(prmDate)=="string"){
prmDate = prmDate.split(/D/);
--prmDate[1];
eval("var prmDate = new Date(" prmDate.join(",") ")");
}
if (typeof(prmDate)=="object"){
var prmDate = prmDate
}
switch(interval){
case "y": prmDate.setFullYear (prmDate.getFullYear() 번호); 부서지다;
케이스 "m": prmDate.setMonth(prmDate.getMonth() 번호); 부서지다;
케이스 "d": prmDate.setDate(prmDate.getDate() 번호); 부서지다;
케이스 "w": prmDate.setDate(prmDate.getDate() 7*number); 부서지다;
케이스 "h": prmDate.setHours(prmDate.getHour() 숫자); 부서지다;
케이스 "n": prmDate.setMinutes(prmDate.getMinutes() 숫자); 부서지다;
케이스 "s": prmDate.setSeconds(prmDate.getSeconds() 번호); 부서지다;
case "l": prmDate.setMilliseconds(prmDate.getMilliseconds() number); 부서지다;
}
prmDate를 반환합니다.
}

/**
* 문자열 길이 가져오기(프로토타입 확장 또는 오버로드)
* @type int
* @returns 문자열 길이
*/
String.prototype.len = function() {
var arr=this.match(/[^x00-xff]/ig );
return this.length (arr==null?0:arr.length);
}

/**
* 문자열 왼쪽에서 지정된 문자 수 가져오기(프로토타입 확장 또는 오버로딩)
* @param {num} 숫자 가져오기
* @type string
* @returns 일치하는 문자열
*/
String.prototype.left = function(num,mode) {
if(!/d /.test(num)) return (이것);
var str = this.substr(0,num);
if(!mode) return str;
var n = str.len() - str.length;
num = num - parsInt(n/2);
return this.substr(0,num);
}

/**
* 문자열 오른쪽에서 지정된 수의 문자 가져오기(프로토타입 확장 또는 오버로딩)
* @param {num} 숫자 가져오기
* @type string
* @returns 일치하는 문자열
*/
String.prototype.right = function(num,mode) {
if(!/d /.test(num)) return (이것);
var str = this.substr(this.length-num);
if(!mode) return str;
var n = str.len() - str.length;
num = num - parsInt(n/2);
return this.substr(this.length-num);
}

/**
* 문자열 포함(프로토타입 확장 또는 오버로드)
* @param {string: str} 검색할 하위 문자열
* @param {bool: mode} 대소문자 무시 여부
* @type int
* @일치 횟수를 반환합니다
*/
String.prototype.matchCount = function(str,mode) {
return eval("this.match(/(" str ") )/g" (모드?"i":"") ").length");
}

/**
* 왼쪽 및 오른쪽 공백 제거(프로토타입 확장 또는 오버로드)
* @type 문자열
* @returns 처리된 문자열
*/
String.prototype.trim = function() {
return this.replace(/(^s*)|(s*$ )/g,"");
}

/**
* 왼쪽 공백 제거(프로토타입 확장 또는 오버로드)
* @type 문자열
* @returns 처리된 문자열
*/
String.prototype.lTrim = function() {
return this.replace(/(^s*)/g, "" );
}

/**
* Remove right space (prototype expansion or overloading)
* @type string
* @returns processed string
*/
String.prototype.rTrim = function() {
return this.replace(/(s*$)/g, "");
}

/**
* Convert string to date type (prototype extension or overloading)
* @type Date
* @returns Date
*/
String.prototype.toDate = function() {
var converted = Date.parse(this);
var myDate = new Date(converted);
if (isNaN(myDate)) {var arys= this.split('-'); myDate = new Date(arys[0],--arys[1],arys[2]); }
return myDate;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template