54.js构造函数原型
作者:互联网
<head>
<meta charset="UTF-8">
<title>3.js对象构造原型</title>
</head>
<body>
<div id="test1"></div>
<div id="test2"></div>
</body>
<script>
//不能在通过构造原型添加内容之前使用
// let date1 = new Date(1998, 8-1, 8, 20, 18, 28);
// document.getElementById("test2").innerHTML = date1.toStringByMy();
//为Date添加格式化方法
Date.prototype.toStringByMy = function () {
let year = this.getFullYear()
, month = this.getMonth() + 1
, date = this.getDate()
, hour = this.getHours()
, min = this.getMinutes()
, sec = this.getSeconds();
let msg = year + "-" + month + "-" + date + " " + hour + ":" + min + ":" + sec;
return msg;
}
//创建日期对象
let now = new Date();
document.getElementById("test1").innerHTML = now.toStringByMy();
let date1 = new Date(1998, 8-1, 8, 20, 18, 28);
document.getElementById("test2").innerHTML = date1.toStringByMy();
</script>
标签:date1,toStringByMy,54,innerHTML,js,let,Date,new,构造函数 来源: https://www.cnblogs.com/wgbl/p/16142130.html