Why doesn't adding a function to the `Date` function add it to the` Date.protoype` in JavaScript?

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).

+4
source share
3 answers

Adding foo to Date.prototype affects all objects created from a Date call as a constructor . Therefore, foo displayed when you say (new Date()).foo() but not Date.foo() - the latter does not call Date as a constructor.

Remember, when you say new Date() , you get an object based on Date prototype . You added foo to the prototype to get foo objects created from new Date()

Adding something directly to Date simply adds it to the Date function. This is why Date.bar() works, but not (new Date()).bar() . bar not in the prototype and, therefore, is not displayed for objects created by calling Date as a constructor.

+6
source

But you don’t actually add foo to Date Prototype, you add it to Date.prototype , which is a prototype of objects built using Date as a constructor function.

From a technical point of view, since Date is a function, you can add your own prototype foo to Date by writing Function.prototype.foo = function() { return 'called foo'; } Function.prototype.foo = function() { return 'called foo'; } ; but obviously this is not what you want.

+2
source

Date.prototype.foo = ... makes foo visible to Date instances through the prototype chain. This means that you only see it in objects where you called Date as a constructor - new Date();

Date.bar = ... sets the bar property of a Date object, which turns out to be a function (remember that all functions are objects in JavaScript). This object has nothing to do with Date instance objects that you call by calling Date as a constructor function.

+2
source

Source: https://habr.com/ru/post/1387129/


All Articles