Anonymous Arrow Functions and Modules

Hi.

Why can’t I assign an anonymous arrow function to a key as a value, inside an object when using module.exports

module.exports={
  myAirplane: "CloudJet",
  displayAirplane: ()=>{
    return this.myAirplane;
  }
};

You should be able to. Is your code raising an error?

1 Like

Yep, my mistake.

I was calling node.js not with its proper name.

Inside an arrow function, this doesn’t refer to the intended object. That code will likely return undefined.

2 Likes

That is invalid code since arrow functions are not permitted in objects. Use the conventional method, or the new ES6 method (preferred).

 displayAirplane () {
    return this.myAirplane
}
1 Like

Got it.

So, just like other method.

1 Like