[英]access parent function inside module.exports
Helo,我对这段代码的问题是我无法访问 checkIf 来设置它的属性 LengthIs,并且当我记录this
是否等于module.exports
,它记录false
。 我也记录了这个,它给了我一些大对象,我无法理解它是什么,也许它是global
。 那么问题是什么?
module.exports = {
checkIf: function(field){
console.log(this === module.exports)
this.checkIf.LengthIs = function(params){
return function(req,res,next){
}
}
}
试试这个( 命名函数表达式):
module.exports = {
checkIf: function checkIf(field){
checkIf.LengthIs = function(params){
return function(req,res,next){
}
}
}
如果您想使用该方法作为提供参数并取回针对该初始参数定制的自定义检查方法,您可以尝试如下所示:
module.exports = {
checkIf: function checkIf(field){
return {
LengthIs: function(params){
// use "field" parameter somewhere in this method
return function(req,res,next){/* whatever.. */ };
}/*,
.. possibly other methods as well */
};
}
}
然后你可以这样做:
this.checkIf("email").LengthIs(xx);
等等..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.