Original question
I realized that I am not 100% sure why this behaves the way I do it, so I decided that this would make a good SO question.
Note:
Date.bar = function() { return 'called bar'; } Date.prototype.foo = function() { return 'called foo'; }
Then in your console, note that these two lines work:
Date.bar(); // 'called bar' (new Date).foo() // 'called foo'
But these two explode and complain that undefined not a function:
Date.foo(); (new Date).bar();
In theory, you should not add a method to the Date prototype so that it is available when the date is canceled? What's going on here?
What I got from your answers and from the game in the console
Basically, Date (i.e. window.Date ) is not a date object. Its function, and it was not built from Date.prototype . (In addition, date objects do not have a prototype property, although of course they still have a prototype chain.) Check this:
typeof Date; // function typeof (new Date); // object Date.prototype.isPrototypeOf(new Date); // true Date.prototype.isPrototypeOf(Date); // false (new Date).prototype; // undefined
Understanding that Date not a date object or prototype for date objects, all this makes much more sense to me. Thank you all.
Also note that Date.prototype and Date.__proto__ different! The first is used when Date run as a function when creating new date objects. The latter is related to the normal prototype chain (and the Date s prototype).
source share