diff --git a/lib/readline.js b/lib/readline.js index 63a7aa816c..a6845010da 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -26,7 +26,10 @@ exports.createInterface = function(input, output, completer, terminal) { function Interface(input, output, completer, terminal) { if (!(this instanceof Interface)) { - return new Interface(input, output, completer, terminal); + // call the constructor preserving original number of arguments + const self = Object.create(Interface.prototype); + Interface.apply(self, arguments); + return self; } this._sawReturn = false; diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index ae8a4188ab..69eb4bf519 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -216,6 +216,18 @@ function isWarned(emitter) { fi.emit('data', ''); // removes listener }); + // calling readline without `new` + fi = new FakeInput(); + rli = readline.Interface({ input: fi, output: fi, terminal: terminal }); + called = false; + rli.on('line', function(line) { + called = true; + assert.equal(line, 'asdf'); + }); + fi.emit('data', 'asdf\n'); + assert.ok(called); + rli.close(); + if (terminal) { // question fi = new FakeInput();