猫鼬模式方法或原型函数?
Mongoose schema methods or prototype functions?
我有一个关于函数和 ES6 的问题 类。我不确定从猫鼬模式创建的新对象是否会复制每个新对象的功能。使用 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);
// 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!')
在决定问我的第一个问题之前,我已经在堆栈溢出和堆栈溢出方面进行了相当多的搜索,但我似乎无法在我的项目中与我发现的问题联系起来,所以我写了这些例子来帮助描述我的问题。
这两个例子都工作正常,我只是不确定模式中的函数是否为我创建的每个新对象复制了,我知道将它添加到原型不会,将它添加到原型是更好的方法并且像这样使用 ES6 类 合适吗?
提前谢谢你,因为我对这个话题很困惑。
更新:
如果其他人带着同样的问题来到这里,在阅读了以下 link 之后,我就明白了。
https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/ch3.md#classes
参考文献
The class syntax is more or less just syntactic sugar for constructor
function + prototype. I.e. the result is (almost) equivalent to
function User(args) {
this.args = args
}
User.prototype.doSomething = function() {
};
// es6 风格
Class User(){
constructor(args){
this.args= args
},
doSomething(){
}
}
两者是等价的
我有一个关于函数和 ES6 的问题 类。我不确定从猫鼬模式创建的新对象是否会复制每个新对象的功能。使用 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);
// 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!')
在决定问我的第一个问题之前,我已经在堆栈溢出和堆栈溢出方面进行了相当多的搜索,但我似乎无法在我的项目中与我发现的问题联系起来,所以我写了这些例子来帮助描述我的问题。
这两个例子都工作正常,我只是不确定模式中的函数是否为我创建的每个新对象复制了,我知道将它添加到原型不会,将它添加到原型是更好的方法并且像这样使用 ES6 类 合适吗?
提前谢谢你,因为我对这个话题很困惑。
更新: 如果其他人带着同样的问题来到这里,在阅读了以下 link 之后,我就明白了。 https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/ch3.md#classes
参考文献
The class syntax is more or less just syntactic sugar for constructor function + prototype. I.e. the result is (almost) equivalent to
function User(args) {
this.args = args
}
User.prototype.doSomething = function() {
};
// es6 风格
Class User(){
constructor(args){
this.args= args
},
doSomething(){
}
}
两者是等价的