【发布时间】:2014-07-17 07:52:58
【问题描述】:
试图给jsDate对象添加一个方法来获取格式化日期,Date没有在任何地方声明或者用new/create()调用:
Date.prototype.format = function () {
// return this.getFullYear() + '-' + ('0' + (this.getMonth() + 1)).slice(-2) + '-' + ('0' + this.getDate()).slice(-2)
var curr_date = this.getDate();
var curr_month = this.getMonth() + 1;
var curr_year = this.getFullYear();
return curr_year + "-" + curr_month + "-" + curr_date;
}
我搞砸了this 变量。
getDate()、getMonth()、getFullYear() 是 Date 对象的方法。
调用.getDate();时抛出错误:undefined is not a function
请说明如何在其原型中使用单一对象类型的其他方法。
【问题讨论】:
-
为我工作:
new Date().format()-"2014-7-17" -
对不起,它也开始为我工作了。
-
这个问题应该被关闭,因为它是由无法再重现的问题引起的。
标签: javascript methods prototype add