Class constructors can not be invoked without 'new'

1

I was trying to make a package of npm , something simple:

class Hello {
    constructor(world){
        this._world = world | 'world';
    }

    world(){
        return this._world;
    }
}

module.exports = Hello;

After doing tests I wanted to initialize it in the following way:

var Hello = require('Hello')('world');
console.log(Hello.world());

But he returned the following error

  

Class constructor Hello can not be invoked without 'new'

What should I change in my module and / or class to initialize it in that way?

And do not have to do something like this:

var Hello = require('Hello');
var hello = new Hello('world');
console.log(hello.world());
    
asked by Ray 02.02.2018 в 15:09
source

1 answer

2

For this you need to instantiate the class from the module accepting the parameter with which you want to use it. Simply change the export line:

...
module.exports = mundo => new Hello(mundo)
    
answered by 02.02.2018 в 15:28