猫鼬的虚拟人vs方法 [英] Virtuals vs Methods in Mongoose
问题描述
我知道static
方法是Class Methods
,methods
是Instance Methods
,并且Virtuals
也类似于Instance Methods
,但是它们没有存储在数据库中.
I understand that static
methods are Class Methods
, and that methods
are Instance Methods
and that Virtuals
are also like Instance Methods
but they are not stored in the database.
但是,我想知道这是否是methods
和virtuals
之间的唯一区别.还有其他我想念的东西吗?
However, I would like to know if that is the only difference between methods
and virtuals
. Is there something else that I'm missing?
推荐答案
实例方法,静态方法或虚拟方法均未存储在数据库中.方法与虚拟函数之间的区别在于,虚拟函数的访问方式类似于属性,而方法的调用方式则类似于函数.实例与静态实例与虚拟实例之间没有区别,因为在类上具有可访问的虚拟静态属性是没有意义的,但在类上具有某些静态实用程序或工厂方法确实是有道理的.
Neither Instance methods, static methods, or virtuals are stored in the database. The difference between methods and virtuals is that virtuals are accessed like properties and methods are called like functions. There's no distinction between instance/static with virtuals because it makes no sense to have a virtual static property accessible on the class, but it does make sense to have some static utility or factory methods on the class.
var PersonSchema = new Schema({
name: {
first: String,
last: String
}
});
PersonSchema.virtual('name.full').get(function () {
return this.name.first + ' ' + this.name.last;
});
var Person = mongoose.model('Person', PersonSchema);
var person = new Person({
name: {
first: 'Alex',
last: 'Ford'
}
});
console.log(person.name.full);
// would print "Alex Ford" to the console
方法的调用方式与普通函数类似.
Whereas methods are called like normal functions.
PersonSchema.method('fullName', function () {
return this.name.first + ' ' + this.name.last;
});
var person = new Person({
name: {
first: 'Alex',
last: 'Ford'
}
});
console.log(person.fullName());
// notice this time you call fullName like a function
您还可以像常规属性一样设置"虚拟属性.只需调用.get
和.set
来设置两个动作的功能.请注意,在.get
中您返回了一个值,而在.set
中您接受了一个值并使用它来设置文档的非虚拟属性.
You can also "set" virtual properties like you're used to with regular properties. Just call .get
and .set
to setup the functionality for both actions. Notice in the .get
you return a value, whereas in the .set
you accept a value and use it to set non-virtual properties on your document.
PersonSchema
.virtual('name.full')
.get(function () {
return this.name.first + ' ' + this.name.last;
})
.set(function (fullName) {
var parts = fullName.split(' ');
this.name.first = parts[0];
this.name.last = parts[1];
});
var person = new Person({
name: {
first: 'Alex',
last: 'Ford'
}
});
console.log(person.name.first);
// would log out "Alex"
person.name.full = 'Billy Bob';
// would set person.name.first and person.name.last appropriately
console.log(person.name.first);
// would log out "Billy"
从技术上讲,您可以对所有方法使用方法,而从不使用虚拟属性,但是对于某些事情,例如我在person.name.full
中显示的示例,虚拟属性很优雅.
You could technically use methods for everything and never use virtual properties, but virtual properties are elegant for certain things such as the examples I've shown here with person.name.full
.
这篇关于猫鼬的虚拟人vs方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!