javascript Sequelize classMethods 与 instanceMethods
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34258938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize classMethods vs instanceMethods
提问by Silent
So starting my adventure into all things Node. One of the tools I am trying to learn is Sequelize. So I will start off what I was trying to do:
所以开始我对 Node.js 的所有事物的冒险。我正在尝试学习的工具之一是 Sequelize。所以我将开始我正在尝试做的事情:
'use strict';
var crypto = require('crypto');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
username: DataTypes.STRING,
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
salt: DataTypes.STRING,
hashed_pwd: DataTypes.STRING
}, {
classMethods: {
},
instanceMethods: {
createSalt: function() {
return crypto.randomBytes(128).toString('base64');
},
hashPassword: function(salt, pwd) {
var hmac = crypto.createHmac('sha1', salt);
return hmac.update(pwd).digest('hex');
},
authenticate: function(passwordToMatch) {
return this.hashPassword(this.salt, passwordToMatch) === this.hashed_pwd;
}
}
});
return User;
};
I am confused on when to use classMethods
vs instanceMethods
. To me when I think about createSalt()
and hashPassword()
should be class methods. They are general and for the most part dont really have anything to do with the specific instance they are just used in general. But when I have createSalt()
and hashPassword()
in classMethods
I cannot call them from instanceMethods
.
我对何时使用classMethods
vs感到困惑instanceMethods
。对我来说,当我想到createSalt()
并且hashPassword()
应该是类方法。它们是通用的,并且在大多数情况下与它们只是一般使用的特定实例没有任何关系。但是当我拥有createSalt()
和hashPassword()
进入时,classMethods
我无法从instanceMethods
.
I have tried variations of the following:
我尝试了以下变体:
this.createSalt();
this.classMethods.createSalt();
createSalt();
Something like below wont work and I am probably just not understanding something simple.
像下面这样的东西不起作用,我可能只是不理解简单的东西。
authenticate: function(passwordToMatch) {
console.log(this.createSalt());
return this.hashPassword(this.salt, passwordToMatch) === this.hashed_pwd;
}
Any hints/tips/direction would be very much so appreciated!
任何提示/提示/方向将非常感谢!
回答by NBeydon
All the method who don't modify or check any type of instance should be classMethod
and the rest instanceMethod
所有不修改或检查任何类型实例的方法应该是classMethod
其余的instanceMethod
ex:
前任:
// Should be a classMethods
function getMyFriends() {
return this.find({where{...}})
}
// Should be a instanceMethods
function checkMyName() {
return this.name === "george";
}
回答by drinchev
Although the basics are that instance
methods should be used when you want to modify your instance
( ergo row ). I would rather not pollute the classMethods
with methods that don't use the class
( ergo the table ) itself.
尽管基本原理是instance
当您想要修改您的instance
(ergo row )时应该使用方法。我宁愿不classMethods
使用不使用class
(ergo table)本身的方法来污染。
In your example I would put hashPassword
function outside your class and leave it as a helper function somewhere in my utilities module ( or why not the same module but as a normal defined function ) ... like
在您的示例中,我会将hashPassword
函数放在您的类之外,并将其作为辅助函数保留在我的实用程序模块中的某处(或者为什么不是同一个模块,而是作为正常定义的函数)......就像
var hashPassword = function(...) { ... }
...
...
instanceMethods: {
authenticate: function( ... ) { hashPassword( ... ) }
}
回答by Mario Olivio Flores
I found this worked for me as of sequelize 3.14
我发现这在 sequelize 3.14 对我有用
var myModel = sequelize.define('model', {
}, {
classMethods: {
someClassMethod: function() {
return true;
}
}, {
instanceMethods: {
callClassMethod: function() {
myModel.someClassMethod();
}
}
});