JS 联接函数(链式函数)


Date.prototype.addDay = function (value) {
this.setDate(this.getDate() + value);
return this;
}
Date.prototype.addMonth = function (value) {
this.setMonth(this.getMonth() + value);
return this;
}
Date.prototype.addYear = function (value) {
this.setFullYear(this.getFullYear() + value);
return this;
}
Date.prototype.formatToYMD=function(date) {
return this.getFullYear() + '-' + (this.getMonth() + 1) + '-' + this.getDate();
}

var ndt = new Date("2015-12-31");
ndt = ndt.addDay(1);
ndt = ndt.addMonth(1);
ndt = ndt.addYear(1);
var ndt2 = new Date("2015-12-31");
ndt2 = ndt2.addYear(1).addDay(1).addMonth(1).formatToYMD();


console.log("明年下个月的明天 " + ndt);
console.log("明年下个月的明天 " + ndt2);

原文地址:https://www.cnblogs.com/denghuachengle/p/4796712.html