0

I'm trying to use the arrow-constructor to create an object:

var Countable = (data) => {
    return data;
}

But when creating an object:

new Countable(newSubscriptions)

I get the error

Uncaught TypeError: (data) => {
    return data;
} is not a constructor

I get the expected output by doing

var Countable = function(data) {
    return data;
}
Himmators
  • 13,568
  • 33
  • 122
  • 214

1 Answers1

1

Yes, you can use an arrow function to create new objects:

var Countable = () => {
    return {}; // This function returns a new object
};
var o = Countable();

However, you can't instantiate an arrow function, because it doesn't have the [[Construct]] internal method. So using new will throw.

Oriol
  • 249,902
  • 55
  • 405
  • 483