为对象添加一个新的方法

例定义一个方法,为Date对象添加一个新的成员方法,转换为形如 y-m-d<br>h:m:s

Date.prototype.stringify = function(){
  var s= this.getFullYear()+'-';
     s+= (this.getMonth()+1)+'-';
     s+= this.getDate()+' ';
     s+= this.getHours()+':';
     s+= this.getMinutes()+':';
     s+= this.getSeconds();
  return s;
}

Date对象可以直接调用stringify()方法

原文地址:https://www.cnblogs.com/7662-scy/p/6501852.html