Javascript Mongoose模式方法还是原型函数?

Javascript Mongoose模式方法还是原型函数?,javascript,node.js,mongoose-schema,Javascript,Node.js,Mongoose Schema,我有一个关于函数和ES6类的问题。我不确定从mongoose模式创建的新对象是否会复制每个新对象的函数。使用ES6类的最佳方法是什么。 我写了一个相关的例子来说明我的意思 // models/User.js const mongoose = require('mongoose'); // User Schema const userSchema = new mongoose.Schema({ name: { type: String, required: true,

我有一个关于函数和ES6类的问题。我不确定从mongoose模式创建的新对象是否会复制每个新对象的函数。使用ES6类的最佳方法是什么。 我写了一个相关的例子来说明我的意思

// 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);



在决定问我的第一个问题之前,我已经在堆栈溢出和关闭上搜索了很多,但我似乎无法在我的项目中与我找到的问题联系起来,所以我写了这些例子来帮助描述我的问题

这两个例子都很好,我只是不确定模式中的函数是否会对我创建的每个新对象进行复制,我知道将其添加到原型中不会,将其添加到原型中是否是更好的方法,并且使用这样的ES6类是否合适

提前谢谢你,因为我对这个话题很困惑

更新: 如果有其他人带着同样的问题来到这里,在阅读了下面的链接后,我只是点击了一下。

参考文献

类语法或多或少只是构造函数的语法糖 功能+原型。也就是说,结果(几乎)相当于

//es6风格

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

  }
}

这两种都是等效的

我明白了,谢谢。我假设,由于它是语法糖,如果我希望在创建实例后向类添加函数,我仍然必须使用User.prototype?
// 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!')
function User(args) {
   this.args = args
}
User.prototype.doSomething = function() { 

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

  }
}