[英]How to access object property in module.exports
I am curious about how to access another object property in module.exports. 我很好奇如何访问module.exports中的另一个对象属性。
here is the case : 情况是这样的:
module.exports = { text: 'abcd', index: (req, res)=>{ console.log(text) <-- is not defined console.log(this.text) <-- undefined } }
well then, how to access the text property?
那么,如何访问text属性? thanks guys need your explanation.
谢谢你们需要你的解释。
Javascript does not have a built-in way to refer to other properties in the same object. Javascript没有内置的方式来引用同一对象中的其他属性。 There are good reasons why it cannot do this for any arbitrary property.
有充分的理由为什么它不能对任何任意属性执行此操作。 So, you either have to make sure that
this
has the right object value in it or you need to save the appropriate object reference yourself somewhere that you can get to it. 所以,你要么必须确保
this
有它的正确对象的值或者您需要保存适当的对象引用自己的地方,你可以得到它。
Here's a way where you save the object reference yourself which works fine for a singleton object: 这是您自己保存对象引用的一种方法,该方法对于单例对象可以正常工作:
let myObj = {
text: 'abcd',
index: (req, res)=>{
console.log(myObj.text)
}
}
module.exports = myObj;
If you know that .index()
will always be called appropriately as a method on your module.exports
(which would be the usual case), then you can stop using the =>
definition and use a normal function
definition (which should pretty much always be used for method declarations) and then this
will have the desired value. 如果您知道
.index()
将始终作为module.exports
上的方法被适当地调用(这是通常的情况),那么您可以停止使用=>
定义并使用普通的function
定义(这应该非常总是被用于方法声明),然后this
将具有所需的值。
module.exports = {
text: 'abcd',
index: function(req, res) {
console.log(this.text)
}
}
This will work as long as .index()
index is called like this: 只要像这样调用
.index()
索引,此方法就可以工作:
let myModule = require('myModule');
myModule.index(req, res);
People tend to fall in love with the arrow syntax and forget that it should pretty much NEVER be used for method definitions because it does NOT set this
to the host object which creates problems for methods. 人们往往在爱上箭头语法和忘记,它应该几乎从不使用方法定义,因为它不设置
this
为其创建方法问题的宿主对象。 Instead, use the regular function
definition for methods of an object. 而是将常规
function
定义用于对象的方法。
Arrow functions are typically very useful for callback functions where you want the callback to have access to the this
value from your environment (called the lexical value of this
). 箭头函数通常对于希望回调能够从您的环境访问
this
值(称为this
的词法值)的回调函数非常有用。 Here are some useful examples: 以下是一些有用的示例:
class Timer {
delay(t, cb) {
this.timer = setTimeout(() => {
// preserve this value inside a callback
this.timer = null;
cb();
})
}
}
Or 要么
// preserve this value inside a callback
let filtered = myArray.filter(item => {
return item.id !== this.master.id;
});
On the other hand, you pretty much never want to use an arrow declaration for a method because that will override the usual object value for this
and replace it with a lexical value for this
. 在另一方面,你几乎从来没有想用一个箭头声明的方法,因为这将覆盖通常对象值
this
与一个词汇值替换this
。
Try this 尝试这个
module.exports = {
text: 'abcd',
index: function (req,res) {
console.log(this.text)
}
}
accessing via 通过访问
var te=require('modulename')
te.index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.