Javascript 访问module.exports内的父函数
Helo,这段代码的问题是我无法访问checkIf来设置它的属性LengthIs,当我记录这是否等于module.exports时,它记录为false。我也记录了这个,它给了我一些大对象,我无法得到它是什么,也许它是全球性的。那么问题是什么呢Javascript 访问module.exports内的父函数,javascript,node.js,Javascript,Node.js,Helo,这段代码的问题是我无法访问checkIf来设置它的属性LengthIs,当我记录这是否等于module.exports时,它记录为false。我也记录了这个,它给了我一些大对象,我无法得到它是什么,也许它是全球性的。那么问题是什么呢 module.exports = { checkIf: function(field){ console.log(this === module.exports) this.checkIf.LengthIs = fu
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){
return {
LengthIs: function(params){
// use "field" parameter somewhere in this method
return function(req,res,next){/* whatever.. */ };
}/*,
.. possibly other methods as well */
};
}
}
然后你可以做:
this.checkIfemail.LengthIsxx;等等..它解决了这个问题,但是,当我使用checkif作为中间件并尝试访问LengthIs时,它返回未定义的内容,如:checkif.LengthIs,也如:checkif'email.LengthIs,在将此函数添加到check之前,需要先调用它。如果您能更具体一些,请使用您的确切问题更新您的问题,以便我能更具体一些。但它很简单;lenge每次调用checkIf函数时,都会将此属性添加到该函数中,因此初始值checkIf.leng此属性确实未定义,需要先调用它。否则,用另一种方式定义属性,使其始终存在我只是认为checkIf'email'调用了它,我可以访问它
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 */
};
}
}