最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to inherit from Javascript Date Object? - Stack Overflow

programmeradmin1浏览0评论

I am trying to create and object that inherits from the Date object.

Below is a Firebug transcript of what I am trying to do.

>>> date_son = Object.create( Date )
Function {}
>>> typeof date_son
"object"
>>> date_son.gettime
undefined
>>> date_son.prototype.getTime
getTime()

I use Object.create to create an object date_son which inherits from Date. The getTime function/attribute is available on date_son.protype, but not on the date_son object itself.

I am clearly doing something wrong. Can anyone point me in the right direction on how to create an object that inherits from Date so that date_son.getTime() is available on the date_son object.

I do not wish to extend Date directly, because I think messing/changing globals is bad programming practice.

I am trying to create and object that inherits from the Date object.

Below is a Firebug transcript of what I am trying to do.

>>> date_son = Object.create( Date )
Function {}
>>> typeof date_son
"object"
>>> date_son.gettime
undefined
>>> date_son.prototype.getTime
getTime()

I use Object.create to create an object date_son which inherits from Date. The getTime function/attribute is available on date_son.protype, but not on the date_son object itself.

I am clearly doing something wrong. Can anyone point me in the right direction on how to create an object that inherits from Date so that date_son.getTime() is available on the date_son object.

I do not wish to extend Date directly, because I think messing/changing globals is bad programming practice.

Share Improve this question edited Aug 21, 2011 at 21:57 p.campbell 101k70 gold badges262 silver badges326 bronze badges asked Aug 21, 2011 at 21:53 user904827user904827 812 bronze badges 4
  • 2 Javascript is case-sensitive. You are contrasting date_son.gettime with date_son.prototype.getTime; notice the difference in capitalization. – Karl Knechtel Commented Aug 21, 2011 at 22:00
  • 2 It'll still be undefined because Date is a Function object, not a Date object. – user113716 Commented Aug 21, 2011 at 22:04
  • 1 You can extend from Date.prototype instead, and that will give you the methods, but I don't know what good that will do you, because they won't be operating on a Date object when you invoke them. – user113716 Commented Aug 21, 2011 at 22:06
  • Or better yet, extend new Date(), but of course you run into the same problems. – Domenic Commented Aug 21, 2011 at 22:49
Add a ment  | 

3 Answers 3

Reset to default 2

There is a note on the MDN Docs by Mozilla (https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Date):

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.

Object.create expects an instance when a parameter is given. Date is no instance on its own and will therefore not work with Object.create.

Furthermore, to address your issue with extending Date directly:

Extending native objects is something which is done regularly. MooTools does it for instance with their own .implement method:

https://github./mootools/mootools-more/blob/master/Source/Types/Date.js#L55

Tutorial about extending natives:

http://net.tutsplus./tutorials/javascript-ajax/quick-tip-how-to-extend-built-in-objects-in-javascript/

Edit:

And an article about the presumed evils of extending natives: http://perfectionkills./extending-built-in-native-objects-evil-or-not/

You could do this:

var date_son = Object.create( Date.prototype );

but it wouldn't seem to do much good because you'll be calling those methods on something other than a Date.

console.log( date_son.getTime() );  // won't work

You could call it like this, but I don't know why you'd want to:

var date_son = Object.create( Date.prototype );

var new_date = new Date();

console.log( date_son.getTime.call( new_date ) );  // 1313964470748

If you want to add some Date utilities, and you don't want to extend Date.prototype, then just create a library of methods that accept a Date object.

var date_son = {
    some_method: function( d ) {
        // do something with the date
        return d;
    }
};

var date = date_son.some_method( new Date );

Good news! This works with the new class-based syntax introduced in ES5.

class DateSon extends Date {
    equals(other) {
        if (typeof other === "object") {
            return this.getTime() === other.getTime();
        } else {
            return false;
        }
    }

    copy() {
        return new Date(this.getTime());
    }
}

dateSon1 = new DateSon();
dateSon2 = dateSon1.copy();
dateSon2.setDate(dateSon1.getDate() + 1);

console.log(dateSon1.equals(dateSon2));
发布评论

评论列表(0)

  1. 暂无评论