0

i am learning about mongoose from the book mastering mongoose by valeri karpov. he states the following.

// Calling `conn.model()` creates a new model. In this book
// a "model" is a class that extends from `mongoose.Model`
const MyModel = conn.model('ModelName', schema);
Object.getPrototypeOf(MyModel) === mongoose.Model; // true

i dont understand how a class which is as i understand it is a constructor function can have a prototype other than: "Function.prototype". (i am talking about the actual prototype and not the prototype property)

just to make it explicit that MyModel is a class/constructor he goes on to use it like so:

const document = new MyModel();

i have reviewed my understanding of protoypal inheritance in javascript and nothing has came to light that explains this.

can anyone explain what is going on here.

user2897377
  • 163
  • 1
  • 9
  • Every `class extends mongoose.Model` inherits from the `Model` class itself (which in term inherits from `Function.prototype`) – Bergi May 17 '21 at 13:05
  • See also https://stackoverflow.com/q/37926910/104857 – Bergi May 17 '21 at 21:04

1 Answers1

0

My confusion came from the fact that i never realised a subtle difference between how inheritance is implemented in es6 classes and how 'I' would implement it using constructor functions.

this article helped me find out what i needed to know: https://www.taniarascia.com/understanding-classes-in-javascript/

borrowing the example from the article:

if we implement inheritance with constructor functions like so

 function Hero(name, level) {
          this.name = name
          this.level = level
      }

      // Adding a method to the constructor
      Hero.prototype.greet = function () {
          return `${this.name} says hello.`
      }

      // Creating a new constructor from the parent
      function Mage(name, level, spell) {
          // Chain constructor with call
          Hero.call(this, name, level)

          this.spell = spell
      }

      // Creating a new object using Hero's prototype as the prototype for the newly created object.
      Mage.prototype = Object.create(Hero.prototype)


      console.log(Object.getPrototypeOf(Mage))// logs - ƒ () { [native code] }

there is no reason to expect the prototype of mage to be other than - ƒ () { [native code] } and indeed it is not

however using classes something is done under the hood i was not aware of:

class Hero {
         constructor(name, level) {
             this.name = name
             this.level = level
         }

         // Adding a method to the constructor
         greet() {
             return `${this.name} says hello.`
         }
     }

     // Creating a new class from the parent
     class Mage extends Hero {
         constructor(name, level, spell) {
             // Chain constructor with super
             super(name, level)

             // Add a new property
             this.spell = spell
         }
     }

     console.log(Object.getPrototypeOf(Mage)) // logs the hero constructor
user2897377
  • 163
  • 1
  • 9