How to extend the Javascript Date object?

前端 未结 12 2102
失恋的感觉
失恋的感觉 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条回答
  •  甜味超标
    2020-12-08 15:54

    Check out the MDC docs on Date specifically:

    Note: Note that Date objects can only be instantiated by calling Date or using it as a constructor; unlike other JavaScript object types, Date objects have no literal syntax.

    It seems like the Date object isn't really a JS object at all. When I was writing an extension library, I ended up doing the following:

    function MyDate() {
       var _d=new Date();
       function init(that) {
          var i;
          var which=['getDate','getDay','getFullYear','getHours',/*...*/,'toString'];
          for (i=0;i

    At least I did that first. The limitations of the JS Date object in the end got the better of me and I switched to my own data storage approach (eg. why does getDate=day of year?)

提交回复
热议问题