繁体   English   中英

无法理解猫鼬虚拟中的“.this”

[英]can't understand ".this" in mongoose virtual


userSchema.virtual("password")
      .set(function(password) {
          //temp var
          this._password = password;

          //gen salt
          this.salt = this.makeSalt();

          //encrypt pass
          this.hashed_password = this.encryptPassword(password);

      })
      .get(function () {
          return this._password;
      });


     
   



我想知道上面代码中“this”所指的范围。我有一个名为 userSchema 的 mongo 模式,我正在为其设置一个虚拟字段,即密码。函数调用 this 指的是全局作用域。

这是指目标字段。

附带说明一下,如果您尝试加密密码,我会使用“pre”而不是虚拟来存储加密密码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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