sequilizejs 中 classMethods 与 instanceMethods 的用法?
usage of classMethods vs instanceMethods in sequilizejs?
我是 sequilizejs 的新手,基本上是在尝试重构我在控制器中编写并遇到 classMethods 和 instanceMethods 的代码。我看到这样定义的实例方法:
/lib/model/db/users.js
module.exports = function(sequelize, DataTypes) {
var instance_methods = get_instance_methods(sequelize);
var User = sequelize.define("User", {
email : {
type : DataTypes.STRING,
allowNull : false
},
}, {
classMethods: class_methods,
instanceMethods : instance_methods,
});
return User;
};
function get_instance_methods(sequelize) {
return {
is_my_password : function( password ) {
return sequelize.models.User.hashify_password( password ) === this.password;
},
};
function get_class_methods(sequelize) {
return {
hashify_password : function( password ) {
return crypto
.createHash('md5')
.update(
password + config.get('crypto_secret'),
(config.get('crypto_hash_encoding') || 'binary')
)
.digest('hex');
},
};
我对上面的理解是 classMethods
是为整个模型定义的通用函数,而 instanceMethods
基本上是对 table/model 中给定行的引用,我说得对吗在假设这个?这是我的首要问题。
此外,我在文档 HERE. I only found this previous answer 中没有看到 classMethods 和 instanceMethods 的任何参考。这提供了对 instanceMethods 和 classMethods.
之间差异的全面理解
基本上,我只是想确认我的理解是否符合 class 与实例方法的预期用途,并且非常感谢指向官方文档的链接。
添加静态方法和实例方法的官方方法是使用 类,如下所示:
class User extends Model {
static classLevelMethod() {
return 'foo';
}
instanceLevelMethod() {
return 'bar';
}
getFullname() {
return [this.firstname, this.lastname].join(' ');
}
}
User.init({
firstname: Sequelize.TEXT,
lastname: Sequelize.TEXT
}, { sequelize });
你的理解是正确的。简而言之:classes 可以有实例。模型是 classes。所以,模型可以有实例。使用实例方法时,您会注意到 this
— 这是上下文,它指的是特定的 class/model 实例。
因此,如果您的 User
模型具有:
- 一个名为
is_my_password
的实例方法
- 一个名为
hashify_password
的 class 模型
User.hashify_password('123')
将 return 123
的散列版本。这里 User
实例 不需要 。 hashify_password
是附加到 User
模型 (class) 的通用函数。
现在,如果您想调用 is_my_password()
,您 do 需要一个 User
实例 :
User.findOne({...}).then(function (user) {
if (user.is_my_password('123')) {
// ^ Here we call `is_my_password` as a method of the user instance.
...
}
}).catch(console.error)
一般来说,当您拥有不需要特定模型实例数据的函数时,您会将它们定义为 class 方法。它们是静态方法。
并且当函数与实例数据一起工作时,您将其定义为实例方法以使其更容易和更好地调用。
我是 sequilizejs 的新手,基本上是在尝试重构我在控制器中编写并遇到 classMethods 和 instanceMethods 的代码。我看到这样定义的实例方法:
/lib/model/db/users.js
module.exports = function(sequelize, DataTypes) {
var instance_methods = get_instance_methods(sequelize);
var User = sequelize.define("User", {
email : {
type : DataTypes.STRING,
allowNull : false
},
}, {
classMethods: class_methods,
instanceMethods : instance_methods,
});
return User;
};
function get_instance_methods(sequelize) {
return {
is_my_password : function( password ) {
return sequelize.models.User.hashify_password( password ) === this.password;
},
};
function get_class_methods(sequelize) {
return {
hashify_password : function( password ) {
return crypto
.createHash('md5')
.update(
password + config.get('crypto_secret'),
(config.get('crypto_hash_encoding') || 'binary')
)
.digest('hex');
},
};
我对上面的理解是 classMethods
是为整个模型定义的通用函数,而 instanceMethods
基本上是对 table/model 中给定行的引用,我说得对吗在假设这个?这是我的首要问题。
此外,我在文档 HERE. I only found this previous answer
基本上,我只是想确认我的理解是否符合 class 与实例方法的预期用途,并且非常感谢指向官方文档的链接。
添加静态方法和实例方法的官方方法是使用 类,如下所示:
class User extends Model {
static classLevelMethod() {
return 'foo';
}
instanceLevelMethod() {
return 'bar';
}
getFullname() {
return [this.firstname, this.lastname].join(' ');
}
}
User.init({
firstname: Sequelize.TEXT,
lastname: Sequelize.TEXT
}, { sequelize });
你的理解是正确的。简而言之:classes 可以有实例。模型是 classes。所以,模型可以有实例。使用实例方法时,您会注意到 this
— 这是上下文,它指的是特定的 class/model 实例。
因此,如果您的 User
模型具有:
- 一个名为
is_my_password
的实例方法
- 一个名为
hashify_password
的 class 模型
User.hashify_password('123')
将 return 123
的散列版本。这里 User
实例 不需要 。 hashify_password
是附加到 User
模型 (class) 的通用函数。
现在,如果您想调用 is_my_password()
,您 do 需要一个 User
实例 :
User.findOne({...}).then(function (user) {
if (user.is_my_password('123')) {
// ^ Here we call `is_my_password` as a method of the user instance.
...
}
}).catch(console.error)
一般来说,当您拥有不需要特定模型实例数据的函数时,您会将它们定义为 class 方法。它们是静态方法。
并且当函数与实例数据一起工作时,您将其定义为实例方法以使其更容易和更好地调用。