Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access object property in module.exports

I am curious about how to access another object property in 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? thanks guys need your explanation.

like image 468
Good Day Avatar asked Jan 14 '17 05:01

Good Day


People also ask

How do you access the properties of an object with a variable?

Answer: Use the Square Bracket ( [] ) Notation There are two ways to access or get the value of a property from an object — the dot ( . ) notation, like obj. foo , and the square bracket ( [] ) notation, like obj[foo] .

Are module exports objects?

module. Exports is the object that is returned to the require() call. By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.


1 Answers

Javascript does not have a built-in way to refer to other properties in the same object. 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.

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.

module.exports = {

 text: 'abcd',
 index: function(req, res) {
   console.log(this.text)
 }
}

This will work as long as .index() index is called like this:

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. Instead, use the regular function definition for methods of an object.


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). 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.

like image 122
jfriend00 Avatar answered Oct 04 '22 15:10

jfriend00