你瞅啥呢

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 @   叶乘风  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2022-05-07 2022-05-07 根据键值对来判断对象长度
点击右上角即可分享
微信分享提示