search

Home  >  Q&A  >  body text

javascript - One about date expansion: get the date after adding numbers to the date, year, month, day, etc.; this code is not very clear? ? ?

Date.prototype.dateAdd = function (interval, number) {

var d = this;
var k = { 'y': 'FullYear', 'q': 'Month', 'm': 'Month', 'w': 'Date', 'd': 'Date', 'h': 'Hours', 'n': 'Minutes', 's': 'Seconds', 'ms': 'MilliSeconds' };
var n = { 'q': 3, 'w': 7 };
eval('d.set' + k[interval] + '(d.get' + k[interval] + '()+' + ((n[interval] || 1) * number) + ')');
return d;

}

The above extension code is not very clear. Can someone explain it to me? ? ? The meaning of each line

伊谢尔伦伊谢尔伦2754 days ago423

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:14:32

    eval(string) The eval() function can calculate a string and execute the JavaScript code in it.

    d is the date object produced by new.

    The entire code is just a spelling js script string and then executed.

    reply
    0
  • Cancelreply