/**
* 给字符串原型对象上添加 方法 可全局使用
*
* var str = "13212345678";
* str.isMobile() // true
*
* **/
String.prototype.isMobile = function () {
let reg = /^1[3-9]\d{9}$/;
//let bool = reg.test(this);
return reg.test(this);//s.match(/^1[3-9]\d{9}$/)
};
Number.prototype.isMobile = function () {
let reg = /^1[3-9]\d{9}$/;
//let bool = reg.test(this);
return reg.test(String(this));
};
//数字
String.prototype.isNumber = function () {
return this.match(/^\d+$/g)
};
Number.prototype.isNumber = function () {
return String(this).match(/^\d+$/g)
};
//密码
String.prototype.isPassword = function () {
return this.match(/^[A-Za-z0-9]{6,15}$/)
};
//验证码
String.prototype.isVerifyCode = function () {
return this.match(/^[0-9]{4}$/)
};
//判断是否是两位
Number.prototype.twoLength = function () {
return this < 9 ? "0" + this : this;
};
String.prototype.twoLength = function () {
return this.length <= 1 ? "0" + this : this;
};
/**
* 格式化时间 正对字符串获得时间的
* 获得 YYYY-MM-DD HH-MM-SS
* var times = "2019-05-13T17:29:35+08:00";
* var b = times.getDateTime(); //2019-5-13 17:29:35
* **/
String.prototype.getDateTime = function () {
if (this !== '') {
let date = new Date(this);
let y = date.getFullYear();
let m = date.getMonth() + 1;
let d = date.getDate();
let h = date.getHours();
let min = date.getMinutes();
let sec = date.getSeconds();
return y.twoLength() + "-" + m.twoLength() + "-" + d.twoLength() + " " + h.twoLength() + ":" + min.twoLength() + ":" + sec.twoLength();
} else {
return '';
}
};
/**
* 和上面方法一样
* 这个是正对时间格式的
* **/
Date.prototype.getDateTime = function () {
let y = this.getFullYear();
if (y > 0) { //有效的格式 才有返回 否则返回空字符串
let m = this.getMonth() + 1;
let d = this.getDate();
let h = this.getHours();
let min = this.getMinutes();
let sec = this.getSeconds();
return y.twoLength() + "-" + m.twoLength() + "-" + d.twoLength() + " " + h.twoLength() + ":" + min.twoLength() + ":" + sec.twoLength();
} else {
return '';
}
};
/**
* 字符串 获得 YYYY-MM-DD
* var times = "2019-05-13T17:29:35+08:00";
* var b = times.getDate(); //2019-5-13
* **/
String.prototype.getDates = function () {
if (this !== '') {
let date = new Date(this);
let y = date.getFullYear();
let m = date.getMonth() + 1;
let d = date.getDate();
return y.twoLength() + "-" + m.twoLength() + "-" + d.twoLength();
} else {
return '';
}
};
/***
*
* 时间格式 获得 YYYY-MM-DD
*
* var times = "2019-05-13T17:29:35+08:00";
* var a = new Date(times); //先转时间格式
* var b = a.getDate(); //2019-5-13
* **/
Date.prototype.getDates = function () {
let y = this.getFullYear();
if (y > 0) { //有效的格式 才有返回 否则返回空字符串
let m = this.getMonth() + 1;
let d = this.getDate();
return y.twoLength() + "-" + m.twoLength() + "-" + d.twoLength();
} else {
return '';
}
};
/**
* 判断是否同一天
* 是同一天 返回 0
* date1 > date2 返回正数
* date1 < date2 返回负数
* ***/
Date.prototype.isSameDay = function (date) {
let date1 = this.getDates(); // 获得 YYYY-MM-DD
let date2 = date.getDates();
return new Date(date1) - new Date(date2);
};
/**
* 判断是否同一天
* 是同一天 返回 0
* date1 > date2 返回正数
* date1 < date2 返回负数
* ***/
String.prototype.isSameDay = Date.prototype.isSameDay;
// function(date){
// let date1 = this.getDates(); // 获得 YYYY-MM-DD
// let date2 = date.getDates();
// return new Date(date1) - new Date(date2);
// };
/**
* 获得星期几
*
* let a = new Date();
* a.getWeek;
* **/
Date.prototype.getWeek = function () {
let n = this.getDay();
let arr = ["日", "一", "二", "三", "四", "五", "六"];
return "星期" + arr[n];
};
/**
* var times = "2019-05-13T17:29:35+08:00";
* times.getWeek()
* **/
String.prototype.getWeek = function () {
let n = new Date(this).getDay();
let arr = ["日", "一", "二", "三", "四", "五", "六"];
return "星期" + arr[n];
};
/**
* 根据传进来的 “毫秒” 获得 时间hh : mm : ss
*
* showHour 是否显示小时 true:显示 false :不显示
* **/
String.prototype.getTime = function (showHour = true) {
let seconds = parseInt(this / 1000);
let minute = 0;
let hour = 0;
if (seconds > 60) {
minute = parseInt(seconds / 60);
seconds = parseInt(seconds % 60);
if (minute > 60) {
hour = parseInt(minute / 60);
minute = parseInt(minute % 60);
}
}
let h = hour.toString();
let m = minute.toString();
let s = seconds.toString();
if (showHour) {
return h + ':' + m + ':' + s;
} else
return m + ':' + s;
};
Number.prototype.getTime = String.prototype.getTime;
/**
* 数组的复制
* var a = [1,2,3,4,5];
* var b = a.copy();
* for in 会遍历到copy
* 进行了扩展 就要用 for 循环
* **/
// Document.prototype.copy = function () {
// return this.map((e) => {
// if (typeof e === 'object') {
// return Object.assign({}, e);
// } else {
// return e;
// }
// })
// };
/**
* 获得 当前周 第n天的 日期
* date.getWeekDay(0) //这周的第一天 星期日
* date.getWeekDay(6) //这周的最后一天 星期六
* 返回 yyyy-mm-dd
* ***/
Date.prototype.getWeekDay= function(n){
//now.getDay() 获得当前日期是这周的星期几
let d = new Date(this - this.getDay() * 86400000); //得到的是 该日期的第一天
//firstDay.getDate() + i 设置要查询的点数 +0是查第一天 + 6 是本周最后一天
//setDate() 设置一个月的某一天 +0是设置第一天 + 6 是设置本周最后一天
d.setDate(d.getDate() + n);
let mon = Number(d.getMonth()) + 1;
return d.getFullYear() + "-" + mon + "-" + d.getDate();
};
/**
* 字符串日期格式 获得 当前周 第n天的 日期
* **/
String.prototype.getWeekDay = function(n){
let day = new Date(this);
let d = new Date(day - day.getDay() * 86400000);
d.setDate(d.getDate() + n);
let mon = Number(d.getMonth()) + 1;
return d.getFullYear() + "-" + mon + "-" + d.getDate();
};
/**
* 获得该日期 所在月的第一天
* **/
Date.prototype.getMonthStartDate =function(){
let y = this.getFullYear();
let m = this.getMonth()+1;
let date = new Date(y+'/'+m+'/1');
return date.getDates()
};
/**
* 获得该日期 所在月的第一天
* **/
String.prototype.getMonthStartDate =function(){
let day = new Date(this);
let y = day.getFullYear();
let m = day.getMonth()+1;
let date = new Date(y+'/'+m+'/1');
return date.getDates()
};
/**
* 获得该日期 所在月的最后一天
* */
Date.prototype.getMonthEndDate = function () {
let y = this.getFullYear();
let m = this.getMonth()+1;
let d = this.getMonthDays();
let day = new Date(y + "/" + m + "/" + d);
return day.getDates();
};
String.prototype.getMonthEndDate = function () {
let date = new Date(this);
let y = date.getFullYear();
let m = date.getMonth()+1;
let d = date.getMonthDays();
let day = new Date(y + "/" + m + "/" + d);
return day.getDates();
};
/**
* 获得改日期 当月的天数
* 获得 一个月的毫秒数 / 一天的毫秒
*
**/
Date.prototype.getMonthDays = function(){
let y = this.getFullYear();
let m = this.getMonth()+1;
let monthStartDate = new Date(y + "/" + m + "/1");
let monthEndDate = m === 12 ? new Date(y + 1 + "/1/1") : new Date(y + "/" + (m + 1) + "/1");
return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
};
String.prototype.getMonthDays = function(){
let date = new Date(this);
let y = date.getFullYear();
let m = date.getMonth()+1;
let monthStartDate = new Date(y + "/" + m + "/1");
let monthEndDate = m === 12 ? new Date(y + 1 + "/1/1") : new Date(y + "/" + (m + 1) + "/1");
return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
};
js prototype 扩展
最新推荐文章于 2021-01-17 12:28:33 发布
本文详细探讨了JavaScript中的原型(prototype)概念,以及如何通过原型进行对象扩展。我们将会学习到如何利用原型实现继承,理解原型链的工作原理,并通过实例解析相关操作。通过对原型的理解,开发者可以更好地掌握JavaScript的面向对象编程。
摘要由CSDN通过智能技术生成