我用的是代杰斯打字稿。
我想用这样的
export class CustomDate extends dayjs.Dayjs {
format() {
}
}它返回'TypeError:未定义的类扩展值不是构造函数或null‘。
当我搜索这个问题时,出现这个问题的原因是循环依赖,但我无法理解。
这样做不对吗?
发布于 2021-12-27 14:55:27
我挖了一段时间的来源,我认为这是可行的。但是,由于dayjs不正式支持子类化,我不建议这样做。
(您可能不得不继续挖掘源代码,不过:https://github.com/iamkun/dayjs/blob/4a7b7d07c885bb9338514c234dbb708e24e9863e/src/index.js)
/* Extracting the underlying Datejs class */
const Dayjs_proto = Object.getPrototypeOf(dayjs());
const Dayjs = Dayjs_proto.constructor;
Dayjs.prototype = Dayjs_proto;
class CustomDate extends Dayjs {
constructor(date) {
super({
date: date,
args: arguments,
});
}
format() {
}
}
const myCustomDateObject = new CustomDate(new Date());
console.log(myCustomDateObject);<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.10.7/dayjs.min.js"></script>
https://stackoverflow.com/questions/70493722
复制相似问题