你瞅啥呢

2024-05-07 js定义类的方法

一:传统写法
// 定义:
function handleDate(date){
this.idate = new Date(date).getTime();
console.log(this.idate);
this.resolveDate = function() {
console.log('resolveDate',this.idate);
}
}
// 使用:
const getDate = new handleDate('2020-02-02 20:20:20');
getDate; // 1580646020000 this,handleDate = {idate: 1580646020000 }
getDate; // 1580646020000 this,handleDate = {idate: 1580646020000 } 1580646020000
二:es6写法,es6引入了class,下面便是用class来定义类
// 定义:
class handleDate2 {
constructor(date) {
this.idate = date;
}
resolveDate() {
console.log(new Date(this.idate).getTime());
}
}
// 使用:
const getDate2 = new handleDate2('2023-12-13 15:16:17' );
getDate2.resolveDate(); // 1702451777000

 

posted @ 2024-05-07 22:55  叶乘风  阅读(20)  评论(0)    收藏  举报