How to extend the Javascript Date object?

前端 未结 12 2103
失恋的感觉
失恋的感觉 2020-12-08 14:50

I\'m trying to subclass/extend the native Date object, without modifying the native object itself.

I\'ve tried this:

    var util = require(\'util\')         


        
12条回答
  •  旧时难觅i
    2020-12-08 15:48

    Looking at the v8 code, in date.js:

    function DateGetHours() {
      var t = DATE_VALUE(this);
      if (NUMBER_IS_NAN(t)) return t;
      return HOUR_FROM_TIME(LocalTimeNoCheck(t));
    }
    

    And looks like DATE_VALUE is a macro that does this:

    DATE_VALUE(arg) = (%_ClassOf(arg) === 'Date' ? %_ValueOf(arg) : ThrowDateTypeError());
    

    So, seems like v8 won't let you subclass Date.

提交回复
热议问题