在下面的代码中,我看到“this”在 Mongoose 的链接方法中使用。所以,我不知道“this”的上下文是什么以及使用“this”的目的是什么?
PersonSchema
.virtual('name.full')
.get(function () {
return this.name.first + ' ' + this.name.last;
})
.set(function (setFullNameTo) {
var split = setFullNameTo.split(' ')
, firstName = split[0]
, lastName = split[1];
this.set('name.first', firstName);
this.set('name.last', lastName);
});
最佳答案
<小时/>In model middleware functions, this refers to the model.
<小时/>In document middleware functions, this refers to the document.
<小时/>In query middleware functions, this refers to the query.
<小时/>Aggregate middleware executes when you call exec() on an aggregate object. In aggregate middleware, this refers to the aggregation object.
this
的目的是让您可以与数据交互。比如改变里面的一个值或者做其他处理。
关于javascript - Mongoose 方法中这个变量的上下文是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173450/