在javascript中添加方法到对象的不同方法

今天,我看到了一个node.js示例中的以下代码。 让我感到困惑的部分是“userSchema.methods.generateHash = function(password)”。

我认为这只是添加一个新的方法到userSchema对象。 但为什么你需要那里的方法。

这两者有什么区别?

userSchema.generateHash =函数(密码)

要么

userSchema.methods.generateHash =函数(密码)

var userSchema = mongoose.Schema({ local: { email: String, password: String, }, facebook: { id: String, token: String, email: String, name: String, }, twitter: { id: String, token: String, displayName: String, username: String }, google: { id: String, token: String, email: String, name: String } }); userSchema.methods.generateHash = function(password){ return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }; userSchema.methods.validPassword = function(password){ return bcrypt.compareSync(password, this.local.password); }; module.exports = mongoose.model('User', userSchema);