为对象添加一个新的方法

例定义一个方法,为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()方法

posted @ 2017-03-04 16:52  橙子2017  阅读(404)  评论(0编辑  收藏  举报