Date类添加一个新的方法,用prototype

由于Date不是非常的了解,所以在没网络的情况下,难倒了俺,现在在这里做个记录。

<script src="dateEX.js"></script>

window.onload=funtion(){

 var date = new Date(2011,1,1);

 var str = date.toChinaString();

 if(str=='2011年 1月 1日') alert(true);

 else alert(false);

}

请在 dateEX.js 文件里实现方法 toChinaString

Date.prototype.toChinaString = function(){

return (this.getFullYear()+'年 '+this.getMonth()+'月 '+this.getDate()+'日');

}

prototype 向对象添加属性或方法

学习了一下。。。。。

记录交流,如有错,请指正。

原文地址:https://www.cnblogs.com/jackychua/p/3009282.html