I read the book by David Flanagan (JavaScript. Detailed Guide). I met this paragraph:

Objects created using the new keyword and invoking the constructor get the value of the prototype property of the constructor function as a prototype. Therefore, the object created by the expression new Object () inherits the properties of the Object.prototype object as if it was created using the literal in curly brackets {}. Similarly, the prototype of the object created by the expression new Array () is Array.prototype, and the prototype of the object created by the expression new Date () is Date.prototype.

Conducted a test:

enter image description here

As you can see: the prototype of an object created as new Date () is not a Date.prototype at all. Question: What does Flanagan mean here as a "prototype of the created object"?

    2 answers 2

    prototype is, as it were, a repository of general properties for all instances of an object.
    That is, the implementation may not have it (unless they themselves want to be ancestors).

    Almost everything has the __proto__ property, which points to this very storage, this is how prototype inheritance works.

    In the example: date.__proto__ points to Date.prototype , from where it takes properties.
    But date no properties in the prototype, so there is no prototype .

      It is necessary to separate objects , and functions constructors .

      In the quote we are talking about the second:

      as a prototype, the prototype property of the constructor function is obtained

      With reference to an example: there is a Date constructor function, it has a prototype property, which contains methods for working with a date.

      After execution: var date = new Date() , a date object is created.

      The created object does not have a prototype property, so the date.prototype call returns undefiend , as well as a call to any other missing property.

      To obtain a prototype for an object, you can use Object.getPrototypeOf , or by using the __proto__ property, which calls this function internally.

      A few examples:

       var date = new Date(); console.log(Object.getPrototypeOf(date) === Date.prototype); console.log(Object.getPrototypeOf(date) === date.__proto__); var array = []; console.log(Object.getPrototypeOf(array) === Array.prototype); console.log(Object.getPrototypeOf(array) === array.__proto__); var object = {}; console.log(Object.getPrototypeOf(object) === Object.prototype); console.log(Object.getPrototypeOf(object) === object.__proto__);