在一个打算在NodeJS 6中使用的模块中。*我编写了一个通过一些有用的方法扩展内置Date类的类。
class DateEnhanced extends Date {
... some new methods here ...
}
NodeJS 6中的一切正常。*因为它实现了所有ES6功能。
现在我正在尝试重新编写NodeJS 4的类,但是当我应用经典继承模式时我就陷入困境:
function DateEnhanced () { return Date.prototype.constructor.apply( this, arguments ); }
DateEnhanced.prototype = Object.create( Date.prototype );
DateEnhanced.prototype.constructor = DateEnhanced ;
只要我只创建对象,一切正常。但是,只要我想使用Date方法,我就会遇到问题:
let d = new DateEnhanced() ;
let h = d.getHours();
导致异常:
TypeError: this is not a Date object.
at D.getHours (native)
我在模式中尝试了各种修改,但没有任何作用。我该怎么办?