0

I am new to node modules. I tried

module.exports = function (firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.fullName = function () { 
        return this.firstName + ' ' + this.lastName;
    }
}

then

let person1 = new person('Rajesh','Dhoundiyal')

This works. But when i try to use Fat arrow in module.exports it does not works.

e.g. module.exports = (firstName, lastName) => {

This gives error later as person is not a constructor. I am unable to know why it happen. Can anyone please tell me why fat arrow not works here.

raju
  • 6,448
  • 24
  • 80
  • 163
  • Arrow functions do not create their own `this`. The `this` scope in an Arrow function refers to a higher level scope. In other words, the `this` scope is as if there was no Arrow function there. – StackSlave Oct 12 '17 at 01:56
  • If you want to see the only difference between arrow functions and "regular" functions, just console.log(this). – Nick Oct 12 '17 at 02:00

1 Answers1

1

The this in arrow-funciton is not same with other normal function. The this in arrow-funciton is point to the context which the arrow-funciton be defined, so it can not be use as a constructor Please refer to Arrow functions:

An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target.These function expressions are best suited for non-method functions, and they cannot be used as constructors.

sinbar
  • 933
  • 2
  • 7
  • 25