简体   繁体   中英

Mongoose schema methods or prototype functions?

I have a question regarding functions and ES6 classes. I am unsure wether new objects created from a mongoose schema duplicate the functions for each new object. And what the best approach is when using ES6 classes. I wrote a relevant example to show what I mean.

// models/User.js
const mongoose = require('mongoose');

// User Schema
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
  },
});


userSchema.methods.say = function testFunc(text){
  console.log(`${this.name} said ${text}`)
}

module.exports = mongoose.model('User', userSchema);

// models/Animals.js
const mongoose = require('mongoose');

// Animal Schema
class Animal extends mongoose.Schema {
  constructor() {
    const animal = super({
      name: {
        type: String,
        required: true,
        unique: true,
      },
    });
    animal.methods.say = this.say;
  }

  say(text) {
    console.log(`${this.name} says ${text}`)
  }
}

module.exports = mongoose.model('Animal', new Animal)

// First test example
const User = require('./models/User');
const john = new User({name: "John"});
const jane = new User({name: "Jane"});

john.say('Dog goes woof.');
jane.say('Cat goes meow.\n');

User.prototype.write = function(text) {
  console.log(`${this.name} wrote ${text}`);
}

john.write('There\'s just one sound that no one knows.')
jane.write('What does the fox say?\n')

// Second test example
const Animal = require('./models/Animal');
const fox = new Animal({name: "Fox"});

fox.say('Ring-ding-ding-ding-dingeringeding!');

Animal.prototype.screams = function(text) {
  console.log(`${this.name} screams ${text}`);
}

fox.screams('Wa-pa-pa-pa-pa-pa-pow!')

Before deciding to ask my first question I've searched quite a bit both on stack overflow and off but I couldn't seem to relate within my project to the questions I found, so I wrote these examples to help portray my question instead.

Both of these examples work fine, I'm just unsure wether the functions inside the schema's get duplicated for every new object I create, I know that adding it to the prototype doesn't, would adding it to the prototype be the better approach and is using ES6 classes like this appropriate?

Thank you in advance as I'm quite confused about the topic.

Update: If anyone else comes here with the same question, after reading the following link things just clicked for me. https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/ch3.md#classes

from ref. Equivalent of Prototype in ES6

The class syntax is more or less just syntactic sugar for constructor function + prototype. Ie the result is (almost) equivalent to

function User(args) {
   this.args = args
}
User.prototype.doSomething = function() { 

};

// es6 style

Class User(){
   constructor(args){
      this.args= args
   },
  doSomething(){

  }
}

both are equivalent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM